Flutter源码流程
2022-06-03 18:32:13 0 举报
AI智能生成
Flutter源码流程
作者其他创作
大纲/内容
WidgetsFlutterBinding.ensureInitialized()
renderView = RenderView(configuration: createViewConfiguration(), window: window)
WidgetsFlutterBinding.scheduleAttachRootWidget(app)
new RenderObjectToWidgetAdapter<RenderBox>(
container: renderView,
debugShortDescription: '[root]',
child: rootWidget, // MyApp()
)
container: renderView,
debugShortDescription: '[root]',
child: rootWidget, // MyApp()
)
RenderObjectToWidgetAdapter.attachToRenderTree(buildOwner!, null)
element = createElement();
owner.buildScope(
element!,
() => element!.mount(null, null),
);
owner.buildScope(
element!,
() => element!.mount(null, null),
);
element!.mount(null, null)
Element.mount(Element? parent, Object? newSlot)
_parent = parent;
_slot = newSlot;
_lifecycleState = _ElementLifecycle.active;
_depth = _parent != null ? _parent!.depth + 1 : 1;
_slot = newSlot;
_lifecycleState = _ElementLifecycle.active;
_depth = _parent != null ? _parent!.depth + 1 : 1;
if (parent != null) {
_owner = parent.owner;
}
_owner = parent.owner;
}
if (widget.key is GlobalKey) {
owner!._registerGlobalKey(key, this);
}
owner!._registerGlobalKey(key, this);
}
_updateInheritance();
_inheritedWidgets = _parent?._inheritedWidgets
RenderObjectElement.mount(Element? parent, Object? newSlot)
_renderObject = widget.createRenderObject(this)
attachRenderObject(newSlot)
_slot = newSlot;
_ancestorRenderObjectElement = _findAncestorRenderObjectElement();
_ancestorRenderObjectElement?.insertRenderObjectChild(renderObject, newSlot);
if (_findAncestorParentDataElement() != null)
_updateParentData(parentDataElement.widget);
_updateParentData(parentDataElement.widget);
parentDataWidget.applyParentData(renderObject)
_dirty = false
_rebuild()
>> _child = updateChild(_child, widget.child, _rootChildSlot)
>> _child = updateChild(_child, widget.child, _rootChildSlot)
Element.updateChild(Element? currentChild, Widget? newWidget, Object? newSlot)
if (newWidget == null) {
if (child != null)
deactivateChild(child);
return null;
}
if (child != null)
deactivateChild(child);
return null;
}
if (child == null)
newChild = inflateWidget(newWidget, newSlot)
newChild = inflateWidget(newWidget, newSlot)
if (newWidget.key is GlobalKey)
final Element? newChild = _retakeInactiveElement(key, newWidget);
if (newChild != null)
newChild._activateWithParent(this, newSlot);
_parent = parent
_updateDepth(_parent!.depth)
_activateRecursively(this)
element.activate()
_updateInheritance()
didChangeDependencies()
state.activate()
markNeedsBuild()
markNeedsBuild()
element.visitChildren(_activateRecursively)
attachRenderObject(newSlot)
final Element? updatedChild = updateChild(newChild, newWidget, newSlot);
return updatedChild!;
else
Element newChild = newWidget.createElement()
newChild.mount(this, newSlot)
Element.mount
RenderObjectElement.mount
SingleChildRenderObjectElement.mount(Element? parent, Object? newSlot)
_child = updateChild(_child, widget.child, null)
MultiChildRenderObjectElement.mount(Element? parent, Object? newSlot)
final List<Element> children = List<Element>.filled(widget.children.length, _NullElement.instance);
Element? previousChild;
for (int i = 0; i < children.length; i += 1) {
final Element newChild = inflateWidget(widget.children[i], IndexedSlot<Element?>(i, previousChild));
children[i] = newChild;
previousChild = newChild;
}
_children = children;
Element? previousChild;
for (int i = 0; i < children.length; i += 1) {
final Element newChild = inflateWidget(widget.children[i], IndexedSlot<Element?>(i, previousChild));
children[i] = newChild;
previousChild = newChild;
}
_children = children;
ComponentElement.mount(Element? parent, Object? newSlot)
>> _firstBuild()
>> _firstBuild()
StatelessElement._firstBuild()
rebuild()
>> performRebuild()
>>>> _dirty = false;
>>>> _child = updateChild(_child, widget.build(this), slot)
>> performRebuild()
>>>> _dirty = false;
>>>> _child = updateChild(_child, widget.build(this), slot)
StatefulElement._firstBuild()
state.initState()
state.didChangeDependencies()
rebuild()
>> performRebuild()
>> performRebuild()
if (_didChangeDependencies) {
state.didChangeDependencies();
_didChangeDependencies = false;
}
state.didChangeDependencies();
_didChangeDependencies = false;
}
_dirty = false;
_child = updateChild(_child, widget.build(this), slot)
return newChild
if (child != null)
if (child.widget == newWidget)
if (child.slot != newSlot)
updateSlotForChild(child, newSlot)
updateSlotForChild(child, newSlot)
newChild = child
else if (Widget.canUpdate(child.widget, newWidget)
if (child.slot != newSlot)
updateSlotForChild(child, newSlot)
updateSlotForChild(child, newSlot)
child.update(newWidget)
Element.update(covariant Widget newWidget)
>> _widget = newWidget
>> _widget = newWidget
ProxyElement.update(ProxyWidget newWidget)
super.update(newWidget)
updated(oldWidget)
notifyClients(oldWidget)
_dirty = true
rebuild()
StatefulElement.update(StatefulWidget newWidget)
super.update(newWidget)
_dirty = true
state._widget = widget
state.didUpdateWidget(oldWidget)
rebuild()
newChild = child
else
deactivateChild(child)
child._parent = null
child.detachRenderObject()
visitChildren((Element child) {
child.detachRenderObject();
});
child.detachRenderObject();
});
_slot = null
owner!._inactiveElements.add(child)
_deactivateRecursively(element)
element.deactivate()
state.deactivate()
for (final InheritedElement dependency in _dependencies!)
dependency._dependents.remove(this)
dependency._dependents.remove(this)
_inheritedWidgets = null
_lifecycleState = _ElementLifecycle.inactive
element.visitChildren(_deactivateRecursively)
_elements.add(element)
newChild = inflateWidget(newWidget, newSlot)
return newChild
WidgetsFlutterBinding.scheduleWarmUpFrame()
handleBeginFrame()
TRANSIENT FRAME CALLBACKS
handleDrawFrame()
PERSISTENT FRAME CALLBACKS
RendererBinding._handlePersistentFrameCallback
WidgetsBinding.drawFrame()
buildOwner!.buildScope(renderViewElement!)
if (_dirtyElements.isEmpty)
return;
return;
element.rebuild()
Element.rebuild()
ComponentElement.performRebuild()
RenderObjectElement.performRebuild()
widget.updateRenderObject(this, renderObject)
_dirty = false
element._inDirtyList = false
RendererBinding.drawFrame()
pipelineOwner.flushLayout();
RenderObject._layoutWithoutResize()
performLayout()
RenderView.performLayout()
_size = configuration.size;
child!.layout(BoxConstraints.tight(_size),)
RenderObject.layout(Constraints constraints, { bool parentUsesSize = false })
if (!parentUsesSize || sizedByParent || constraints.isTight || parent is! RenderObject) {
relayoutBoundary = this;
} else {
relayoutBoundary = (parent! as RenderObject)._relayoutBoundary;
}
relayoutBoundary = this;
} else {
relayoutBoundary = (parent! as RenderObject)._relayoutBoundary;
}
if (!_needsLayout && constraints == _constraints && relayoutBoundary == _relayoutBoundary) {
return;
}
return;
}
_constraints = constraints;
_relayoutBoundary = relayoutBoundary;
if (_relayoutBoundary != null && relayoutBoundary != _relayoutBoundary) {
visitChildren(_cleanChildRelayoutBoundary);
}
visitChildren(_cleanChildRelayoutBoundary);
}
if (_relayoutBoundary != this) {
_relayoutBoundary = null;
_needsLayout = true;
visitChildren(_cleanChildRelayoutBoundary);
}
_relayoutBoundary = null;
_needsLayout = true;
visitChildren(_cleanChildRelayoutBoundary);
}
if (sizedByParent) {
performResize();
}
performResize();
}
performLayout()
_needsLayout = false;
markNeedsPaint();
_needsLayout = false;
markNeedsPaint();
pipelineOwner.flushCompositingBits();
pipelineOwner.flushPaint();
PaintingContext.repaintCompositedChild(node)
OffsetLayer? childLayer = child._layerHandle.layer as OffsetLayer?;
if (childLayer == null) {
final OffsetLayer layer = OffsetLayer();
child._layerHandle.layer = childLayer = layer;
} else {
childLayer.removeAllChildren();
}
final OffsetLayer layer = OffsetLayer();
child._layerHandle.layer = childLayer = layer;
} else {
childLayer.removeAllChildren();
}
childContext ??= PaintingContext(childLayer, child.paintBounds);
child._paintWithContext(childContext, Offset.zero);
if (_needsLayout)
return;
return;
_needsPaint = false;
paint(context, offset);
RenderView.paint(PaintingContext context, Offset offset)
if (child != null)
context.paintChild(child!, offset);
context.paintChild(child!, offset);
if (child.isRepaintBoundary)
stopRecordingIfNeeded();
_compositeChild(child, offset);
if (child._needsPaint) {
repaintCompositedChild(child, debugAlsoPaintedParent: true);
}
repaintCompositedChild(child, debugAlsoPaintedParent: true);
}
final OffsetLayer childOffsetLayer = child._layerHandle.layer! as OffsetLayer;
childOffsetLayer.offset = offset;
appendLayer(childOffsetLayer);
childOffsetLayer.offset = offset;
appendLayer(childOffsetLayer);
else
child._paintWithContext(this, offset);
Canvas get canvas
if (_canvas == null) _startRecording();
return _canvas!;
return _canvas!;
_currentLayer = PictureLayer(estimatedBounds);
_recorder = ui.PictureRecorder();
_canvas = Canvas(_recorder!);
// 将pictureLayer添加到_containerLayer(绘制边界节点的Layer)中
_containerLayer.append(_currentLayer!);
_recorder = ui.PictureRecorder();
_canvas = Canvas(_recorder!);
// 将pictureLayer添加到_containerLayer(绘制边界节点的Layer)中
_containerLayer.append(_currentLayer!);
childContext.stopRecordingIfNeeded();
if (sendFramesToEngine) {
renderView.compositeFrame(); // this sends the bits to the GPU
pipelineOwner.flushSemantics(); // this also sends the semantics to the OS.
_firstFrameSent = true;
}
renderView.compositeFrame(); // this sends the bits to the GPU
pipelineOwner.flushSemantics(); // this also sends the semantics to the OS.
_firstFrameSent = true;
}
buildOwner!.finalizeTree()
Element.unmount()
if (key is GlobalKey) {
owner!._unregisterGlobalKey(key, this);
}
owner!._unregisterGlobalKey(key, this);
}
_widget = null
_dependencies = null
_lifecycleState = _ElementLifecycle.defunct
POST-FRAME CALLBACKS
if (hadScheduledFrame)
scheduleFrame()
scheduleFrame()
ensureFrameCallbacksRegistered()
window.scheduleFrame()
_hasScheduledFrame = true
setState(VoidCallback fn)
fn()
_element!.markNeedsBuild()
if (dirty)
return;
return;
_dirty = true
owner!.scheduleBuildFor(this)
SchedulerBinding.ensureVisualUpdate()
_dirtyElements.add(element)
element._inDirtyList = true
RenderObject.markNeedsLayout()
if (_needsLayout)
return;
return;
if (_relayoutBoundary != this) {
markParentNeedsLayout();
}
markParentNeedsLayout();
}
_needsLayout = true;
parent.markNeedsLayout();
else {
_needsLayout = true;
if (owner != null) {
owner!._nodesNeedingLayout.add(this);
owner!.requestVisualUpdate();
}
}
_needsLayout = true;
if (owner != null) {
owner!._nodesNeedingLayout.add(this);
owner!.requestVisualUpdate();
}
}
RenderObject.markNeedsPaint()
if (_needsPaint)
return;
return;
_needsPaint = true;
if (isRepaintBoundary) {
if (owner != null) {
owner!._nodesNeedingPaint.add(this);
owner!.requestVisualUpdate();
}
}
if (owner != null) {
owner!._nodesNeedingPaint.add(this);
owner!.requestVisualUpdate();
}
}
else if (parent is RenderObject) {
(this.parent! as RenderObject).markNeedsPaint();
}
(this.parent! as RenderObject).markNeedsPaint();
}
else {
if (owner != null)
owner!.requestVisualUpdate();
}
if (owner != null)
owner!.requestVisualUpdate();
}
0 条评论
下一页