而在 Flutter 中当需要更新 UI 展示的时候,我们第一时间往往想到 setState()
。更新 UI 本质上,不就是用一个新的「帧」去替换上一个「帧」么。所以,其中必定会执行帧的调度逻辑。而 setState
最终调用到 BuildOwner.scheduleBuildFor
。
/// dart
/// Adds an element to the dirty elements list so that it will be rebuilt
/// when [WidgetsBinding.drawFrame] calls [buildScope].
void scheduleBuildFor(Element element) {
.......
if (!_scheduledFlushDirtyElements && onBuildScheduled != null) {
_scheduledFlushDirtyElements = true;
onBuildScheduled();
}
_dirtyElements.add(element);
element._inDirtyList = true;
........
}
方法中有两个关键点:
1、onBuildScheduled()
2、将 element 添加到
_dirtyElements
中