1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
void scheduleAttachRootWidget(Widget rootWidget) { Timer.run(() { attachRootWidget(rootWidget); }); }
void attachRootWidget(Widget rootWidget) { final bool isBootstrapFrame = renderViewElement == null; _readyToProduceFrames = true; _renderViewElement = RenderObjectToWidgetAdapter<RenderBox>( container: renderView, debugShortDescription: '[root]', child: rootWidget, ).attachToRenderTree(buildOwner!, renderViewElement as RenderObjectToWidgetElement<RenderBox>?); if (isBootstrapFrame) { SchedulerBinding.instance!.ensureVisualUpdate(); } }
class RenderObjectToWidgetAdapter<T extends RenderObject> extends RenderObjectWidget { RenderObjectToWidgetElement<T> attachToRenderTree(BuildOwner owner, [ RenderObjectToWidgetElement<T>? element ]) { if (element == null) { owner.lockState(() { element = createElement(); assert(element != null); element!.assignOwner(owner); }); owner.buildScope(element!, () { element!.mount(null, null); }); } else { element._newWidget = this; element.markNeedsBuild(); } return element!; } }
class RenderObjectToWidgetElement<T extends RenderObject> extends RootRenderObjectElement { void mount(Element? parent, Object? newSlot) { assert(parent == null); super.mount(parent, newSlot); _rebuild(); assert(_child != null); }
void _rebuild() { try { _child = updateChild(_child, widget.child, _rootChildSlot); } catch (exception, stack) { final FlutterErrorDetails details = FlutterErrorDetails( exception: exception, stack: stack, library: 'widgets library', context: ErrorDescription('attaching to the render tree'), ); FlutterError.reportError(details); final Widget error = ErrorWidget.builder(details); _child = updateChild(null, error, _rootChildSlot); } }
Element? updateChild(Element? child, Widget? newWidget, Object? newSlot) { if (newWidget == null) { if (child != null) deactivateChild(child); return null; }
final Element newChild; if (child != null) { bool hasSameSuperclass = true; if (hasSameSuperclass && child.widget == newWidget) { if (child.slot != newSlot) updateSlotForChild(child, newSlot); newChild = child; } else if (hasSameSuperclass && Widget.canUpdate(child.widget, newWidget)) { if (child.slot != newSlot) updateSlotForChild(child, newSlot); if (!kReleaseMode && debugProfileBuildsEnabled) { Map<String, String> debugTimelineArguments = timelineArgumentsIndicatingLandmarkEvent; Timeline.startSync( '${newWidget.runtimeType}', arguments: debugTimelineArguments, ); } child.update(newWidget); if (!kReleaseMode && debugProfileBuildsEnabled) Timeline.finishSync(); newChild = child; } else { deactivateChild(child); assert(child._parent == null); if (!kReleaseMode && debugProfileBuildsEnabled) { Map<String, String> debugTimelineArguments = timelineArgumentsIndicatingLandmarkEvent; Timeline.startSync( '${newWidget.runtimeType}', arguments: debugTimelineArguments, ); } newChild = inflateWidget(newWidget, newSlot); if (!kReleaseMode && debugProfileBuildsEnabled) Timeline.finishSync(); } } else { if (!kReleaseMode && debugProfileBuildsEnabled) { Map<String, String> debugTimelineArguments = timelineArgumentsIndicatingLandmarkEvent; Timeline.startSync( '${newWidget.runtimeType}', arguments: debugTimelineArguments, ); } newChild = inflateWidget(newWidget, newSlot); if (!kReleaseMode && debugProfileBuildsEnabled) Timeline.finishSync(); }
return newChild; }
Element inflateWidget(Widget newWidget, Object? newSlot) { final Key? key = newWidget.key; if (key is GlobalKey) { final Element? newChild = _retakeInactiveElement(key, newWidget); if (newChild != null) { newChild._activateWithParent(this, newSlot); final Element? updatedChild = updateChild(newChild, newWidget, newSlot); assert(newChild == updatedChild); return updatedChild!; } } final Element newChild = newWidget.createElement(); newChild.mount(this, newSlot); return newChild; }
}
abstract class RenderObjectElement extends Element { @override void mount(Element? parent, Object? newSlot) { super.mount(parent, newSlot); assert(() { _debugDoingBuild = true; return true; }()); _renderObject = widget.createRenderObject(this); attachRenderObject(newSlot); _dirty = false; } }
abstract class Element extends DiagnosticableTree implements BuildContext { void mount(Element? parent, Object? newSlot) { assert(_lifecycleState == _ElementLifecycle.initial); assert(widget != null); assert(_parent == null); assert(parent == null || parent._lifecycleState == _ElementLifecycle.active); assert(slot == null); _parent = parent; _slot = newSlot; _lifecycleState = _ElementLifecycle.active; _depth = _parent != null ? _parent!.depth + 1 : 1; if (parent != null) { _owner = parent.owner; } assert(owner != null); final Key? key = widget.key; if (key is GlobalKey) { owner!._registerGlobalKey(key, this); } _updateInheritance(); } }
|