赞
踩
/// The logic and internal state for a [StatefulWidget]. /// /// State is information that (1) can be read synchronously when the widget is /// built and (2) might change during the lifetime of the widget. It is the /// responsibility of the widget implementer to ensure that the [State] is /// promptly notified when such state changes, using [State.setState]. /// /// [State] objects are created by the framework by calling the /// [StatefulWidget.createState] method when inflating a [StatefulWidget] to /// insert it into the tree. Because a given [StatefulWidget] instance can be /// inflated multiple times (e.g., the widget is incorporated into the tree in /// multiple places at once), there might be more than one [State] object /// associated with a given [StatefulWidget] instance. Similarly, if a /// [StatefulWidget] is removed from the tree and later inserted in to the tree /// again, the framework will call [StatefulWidget.createState] again to create /// a fresh [State] object, simplifying the lifecycle of [State] objects. /// /// [State] objects have the following lifecycle: /// /// * The framework creates a [State] object by calling /// [StatefulWidget.createState]. /// * The newly created [State] object is associated with a [BuildContext]. /// This association is permanent: the [State] object will never change its /// [BuildContext]. However, the [BuildContext] itself can be moved around /// the tree along with its subtree. At this point, the [State] object is /// considered [mounted]. /// * The framework calls [initState]. Subclasses of [State] should override /// [initState] to perform one-time initialization that depends on the /// [BuildContext] or the widget, which are available as the [context] and /// [widget] properties, respectively, when the [initState] method is /// called. /// * The framework calls [didChangeDependencies]. Subclasses of [State] should /// override [didChangeDependencies] to perform initialization involving /// [InheritedWidget]s. If [BuildContext.dependOnInheritedWidgetOfExactType] is /// called, the [didChangeDependencies] method will be called again if the /// inherited widgets subsequently change or if the widget moves in the tree. /// * At this point, the [State] object is fully initialized and the framework /// might call its [build] method any number of times to obtain a /// description of the user interface for this subtree. [State] objects can /// spontaneously request to rebuild their subtree by callings their /// [setState] method, which indicates that some of their internal state /// has changed in a way that might impact the user interface in this /// subtree. /// * During this time, a parent widget might rebuild and request that this /// location in the tree update to display a new widget with the same /// [runtimeType] and [Widget.key]. When this happens, the framework will /// update the [widget] property to refer to the new widget and then call the /// [didUpdateWidget] method with the previous widget as an argument. [State] /// objects should override [didUpdateWidget] to respond to changes in their /// associated widget (e.g., to start implicit animations). The framework /// always calls [build] after calling [didUpdateWidget], which means any /// calls to [setState] in [didUpdateWidget] are redundant. /// * During development, if a hot reload occurs (whether initiated from the /// command line `flutter` tool by pressing `r`, or from an IDE), the /// [reassemble] method is called. This provides an opportunity to /// reinitialize any data that was prepared in the [initState] method. /// * If the subtree containing the [State] object is removed from the tree /// (e.g., because the parent built a widget with a different [runtimeType] /// or [Widget.key]), the framework calls the [deactivate] method. Subclasses /// should override this method to clean up any links between this object /// and other elements in the tree (e.g. if you have provided an ancestor /// with a pointer to a descendant's [RenderObject]). /// * At this point, the framework might reinsert this subtree into another /// part of the tree. If that happens, the framework will ensure that it /// calls [build] to give the [State] object a chance to adapt to its new /// location in the tree. If the framework does reinsert this subtree, it /// will do so before the end of the animation frame in which the subtree was /// removed from the tree. For this reason, [State] objects can defer /// releasing most resources until the framework calls their [dispose] /// method. /// * If the framework does not reinsert this subtree by the end of the current /// animation frame, the framework will call [dispose], which indicates that /// this [State] object will never build again. Subclasses should override /// this method to release any resources retained by this object (e.g., /// stop any active animations). /// * After the framework calls [dispose], the [State] object is considered /// unmounted and the [mounted] property is false. It is an error to call /// [setState] at this point. This stage of the lifecycle is terminal: there /// is no way to remount a [State] object that has been disposed. ///
上面从framework.dart文件里面截取的关于生命周期描述的部分
/// [State] objects are created by the framework by calling the
/// [StatefulWidget.createState] method when inflating a [StatefulWidget] to
/// insert it into the tree. Because a given [StatefulWidget] instance can be
/// inflated multiple times (e.g., the widget is incorporated into the tree in
/// multiple places at once), there might be more than one [State] object
/// associated with a given [StatefulWidget] instance. Similarly, if a
/// [StatefulWidget] is removed from the tree and later inserted in to the tree
/// again, the framework will call [StatefulWidget.createState] again to create
/// a fresh [State] object, simplifying the lifecycle of [State] objects.
当一个StatefulWidget插入到渲染树结构、或者从渲染树结构移除时,都会调用StatefulWidget.createState方法,从而达到更新UI的效果。
/// The framework calls [initState]. Subclasses of [State] should override
/// [initState] to perform one-time initialization that depends on the
/// [BuildContext] or the widget, which are available as the [context] and
/// [widget] properties, respectively, when the [initState] method is
/// called.
initState是StatefulWidget创建后调用的第一个方法,而且只执行一次。在执行initState时,View没有渲染,但是StatefulWidget 已经被加载到渲染树里了。
/// * The framework calls [didChangeDependencies]. Subclasses of [State] should /// override [didChangeDependencies] to perform initialization involving /// [InheritedWidget]s. If [BuildContext.dependOnInheritedWidgetOfExactType] is /// called, the [didChangeDependencies] method will be called again if the /// inherited widgets subsequently change or if the widget moves in the tree. /// * At this point, the [State] object is fully initialized and the framework /// might call its [build] method any number of times to obtain a /// description of the user interface for this subtree. [State] objects can /// spontaneously request to rebuild their subtree by callings their /// [setState] method, which indicates that some of their internal state /// has changed in a way that might impact the user interface in this /// subtree. /// * During this time, a parent widget might rebuild and request that this /// location in the tree update to display a new widget with the same /// [runtimeType] and [Widget.key]. When this happens, the framework will /// update the [widget] property to refer to the new widget and then call the /// [didUpdateWidget] method with the previous widget as an argument. [State] /// objects should override [didUpdateWidget] to respond to changes in their /// associated widget (e.g., to start implicit animations). The framework /// always calls [build] after calling [didUpdateWidget], which means any /// calls to [setState] in [didUpdateWidget] are redundant.
didChangeDependencies会在initState后立即调用,当StatefulWidget依赖的InheritedWidget发生变化之后,didChangeDependencies会调用,所以didChangeDependencies可以调用多次。
/// * The framework calls [didChangeDependencies]. Subclasses of [State] should
/// override [didChangeDependencies] to perform initialization involving
/// [InheritedWidget]s. If [BuildContext.dependOnInheritedWidgetOfExactType] is
/// called, the [didChangeDependencies] method will be called again if the
/// inherited widgets subsequently change or if the widget moves in the tree.
/// * At this point, the [State] object is fully initialized and the framework
/// might call its [build] method any number of times to obtain a
/// description of the user interface for this subtree.
build方法会在didChangeDeoendencies之后立即调用,在之后setState()刷新时,会重新调用build绘制页面,所以build方法可以调用多次。但一般不再build中创建除创建Widget的方法,否则会影响渲染效率。
/// [State] objects can spontaneously request to rebuild their subtree by callings their
/// [setState] method, which indicates that some of their internal state
/// has changed in a way that might impact the user interface in this
/// subtree.
[State] 对象可以通过调用它们的 [setState] 方法自发地请求重建其子树,这表明它们的某些内部状态已经改变,可能会影响该子树中的用户界面。setState方法会被多次调用。
/// [State] objects can spontaneously request to rebuild their subtree by callings their
/// [setState] method, which indicates that some of their internal state
/// has changed in a way that might impact the user interface in this
/// subtree.
/// * During this time, a parent widget might rebuild and request that this
/// location in the tree update to display a new widget with the same
/// [runtimeType] and [Widget.key]. When this happens, the framework will
/// update the [widget] property to refer to the new widget and then call the
/// [didUpdateWidget] method with the previous widget as an argument. [State]
/// objects should override [didUpdateWidget] to respond to changes in their
/// associated widget (e.g., to start implicit animations). The framework
/// always calls [build] after calling [didUpdateWidget], which means any
/// calls to [setState] in [didUpdateWidget] are redundant.
1、当调用setState更新UI的时候,都会调用didUpdateWidget。
2、框架在调用 [didUpdateWidget] 之后总是调用 [build],在 [didUpdateWidget] 中对 [setState] 的任何调用都是多余的
/// * If the subtree containing the [State] object is removed from the tree /// (e.g., because the parent built a widget with a different [runtimeType] /// or [Widget.key]), the framework calls the [deactivate] method. Subclasses /// should override this method to clean up any links between this object /// and other elements in the tree (e.g. if you have provided an ancestor /// with a pointer to a descendant's [RenderObject]). /// * At this point, the framework might reinsert this subtree into another /// part of the tree. If that happens, the framework will ensure that it /// calls [build] to give the [State] object a chance to adapt to its new /// location in the tree. If the framework does reinsert this subtree, it /// will do so before the end of the animation frame in which the subtree was /// removed from the tree. For this reason, [State] objects can defer /// releasing most resources until the framework calls their [dispose] /// method. /// * If the framework does not reinsert this subtree by the end of the current /// animation frame, the framework will call [dispose], which indicates that /// this [State] object will never build again. Subclasses should override /// this method to release any resources retained by this object (e.g., /// stop any active animations).
1、当框架从树中移除此 State 对象时将会调用此方法,
2、在某些情况下,框架将重新插入 State 对象到树的其他位置(例如,如果包含该树的子树 State 对象从树中的一个位置移植到另一位置),框架将会调用 build 方法来提供 State 对象适应其在树中的新位置。
3、当框架从树中永久移除此 State 对象时将会调用此方法,与 deactivate的区别是,deactivate 还可以重新插入到树中,而 dispose 表示此 State 对象永远不会在 build。调用完 dispose后,mounted 属性被设置为 false,也代表组件生命周期的结束,此时再调用 setState 方法将会抛出异常。子类重写此方法,释放相关资源,比如动画等。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。