/// The current [SchedulerBinding], if one has been created.////// Provides access to the features exposed by this mixin. The binding must/// be initialized before using this getter; this is typically done by calling/// [runApp] or [WidgetsFlutterBinding.ensureInitialized].staticSchedulerBindingget instance =>BindingBase.checkInstance(_instance);staticSchedulerBinding? _instance;
void_ensureEventLoopCallback(){assert(!locked);assert(_taskQueue.isNotEmpty);if(_hasRequestedAnEventLoopCallback){return;}
_hasRequestedAnEventLoopCallback =true;Timer.run(_runTasks);}// Scheduled by _ensureEventLoopCallback.void_runTasks(){
_hasRequestedAnEventLoopCallback =false;if(handleEventLoopCallback()){_ensureEventLoopCallback();}// runs next task when there's time}
c. 取任务队头,并执行任务, 我们可以看到队列的执行是通过_ensureEventLoopCallback和handleEventLoopCallback 形成递归取任务,终止条件是_taskQueue.isEmpty || locked。
// 被调用方voidscheduleFrame(){if(_hasScheduledFrame ||!framesEnabled){return;}ensureFrameCallbacksRegistered();
platformDispatcher.scheduleFrame();
_hasScheduledFrame =true;}voidmain(){// We use ViewRenderingFlutterBinding to attach the render tree to the window.ViewRenderingFlutterBinding(// The root of our render tree is a RenderPositionedBox, which centers its// child both vertically and horizontally.
root:RenderPositionedBox(
child:RenderParagraph(constTextSpan(text:'Hello, world.'),
textDirection:TextDirection.ltr,),),).scheduleFrame();// 调用方}