void setState(VoidCallback fn) { assert(fn != null); finalObject? result = fn() asdynamic; assert(() { if (result is Future) { throw FlutterError.fromParts(<DiagnosticsNode>[ ErrorSummary('setState() callback argument returned a Future.'), ErrorDescription( 'The setState() method on $this was called with a closure or method that ' 'returned a Future. Maybe it is marked as "async".', ), ErrorHint( 'Instead of performing asynchronous work inside a call to setState(), first ' 'execute the work (without updating the widget state), and then synchronously ' 'update the state inside a call to setState().', ), ]); } // We ignore other types of return values so that you can do things like: // setState(() => x = 3); returntrue; }()); _element!.markNeedsBuild(); }
void markNeedsBuild() { if (_lifecycleState != _ElementLifecycle.active) return; if (dirty) return; _dirty = true; owner!.scheduleBuildFor(this); }
如果当前状态不是active则不标记,如果已经标记过也不在标记;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/// Adds an element to the dirty elements list so that it will be rebuilt /// when [WidgetsBinding.drawFrame] calls [buildScope]. void scheduleBuildFor(Element element) {
void initInstances() { super.initInstances(); _instance = this; // Initialization of [_buildOwner] has to be done after // [super.initInstances] is called, as it requires [ServicesBinding] to // properly setup the [defaultBinaryMessenger] instance. _buildOwner = BuildOwner(); buildOwner!.onBuildScheduled = _handleBuildScheduled; platformDispatcher.onLocaleChanged = handleLocaleChanged; platformDispatcher.onAccessibilityFeaturesChanged = handleAccessibilityFeaturesChanged; SystemChannels.navigation.setMethodCallHandler(_handleNavigationInvocation);
staticint _sort(Element a, Element b) { if (a.depth < b.depth) return-1; if (b.depth < a.depth) return1; if (b.dirty && !a.dirty) return-1; if (a.dirty && !b.dirty) return1; return0; }