Flutter - State类

2019-10-09  本文已影响0人  锐心凌志

写了这么多,无非就是State文档贴过来,最有感触的就两点:

  1. State 的生命周期和 StatefulWidget 不同,当 StatefulWidget 状态改变后就会被重建,但是 State 不会改变,但是当 StatefulWidget 在 View 树中移除再插入又会生成新的 State。参考下文 State.context.
  2. initState -> build -> 状态改变 -> didUpdateWidget -> build --->移除.

在学习 flutter 的时候,总会用到 StatefulWidget,它是一个有状态的 widget,会根据不同状态有不同显示,它的生命周期与 State 有关,它的基本写法如下

// flutter官方教程里面的一个类,点击一个关注的星星,然后就表明已经关注。再点击表示不再关注,默认状态是已关注,关注数是41
class FavoriteWidget extends StatefulWidget {
  @override
  _FavoriteWidgetState createState() => new _FavoriteWidgetState();
}
class _FavoriteWidgetState extends State<FavoriteWidget> {
  bool _isFavorited = true;
  int _favoriteCount = 41;

  void _toggleFavorite() {
    setState(() {
      // If the lake is currently favorited, unfavorite it. Otherwise, favorite it.
        _favoriteCount = _isFavorited?_favoriteCount -1:_favoriteCount +1 ;
        _isFavorited = !_isFavorited;
    });
  }
  @override
  Widget build(BuildContext context) {
    return new Widget(
        // build by states
    );
  }
}

将 FavoriteWidget 插入到 Views 树中的时候,每当调用 States.setState() 时,都会重新build一次FavoriteWidget ,然后将新的 Views代替原先的,并显示给用户。

您可能想知道为什么 StatefulWidget 和 State 是单独的对象。在 Flutter 中,这两种类型的对象具有不同的生命周期: Widget 是临时对象,用于构建当前状态下的应用程序,而 State 对象在多次调用build()之间保持不变,允许它们记住信息(状态)。

那么首先得知道,State的生命周期是什么?

一、生命周期

突然看到这个图,可能会有点懵,因为许多方法都不清是干什么的,稍后会慢慢讲解,不耐烦的可以跳到最后每个方法的总结,这里先总体介绍一下生命周期,大致可以看成三个阶段:

先举 FavoriteWidget 为例,当这个 Widget 首次插入到树中时,框架会调用其 createState 函数以创建一个新的_FavoriteWidgetState实例来与该树中的相应位置关联(请注意,我们通常命名State子类时带一个下划线,这表示其是私有的)。 当这个widget的父级重建时,父级将创建一个新的FavoriteWidget实例,但是Flutter框架将重用已经在树中的_FavoriteWidgetState实例,而不是再次调用createState创建一个新的。

从 StatefulWidget 调用createState之后,框架将新的状态对象插入树中,然后调用 State 的initState,接着如上图所示走了一遍自身的生命周期。需要注意的是,StatefulWidget 和 State 是不同的生命周期。

二、State 源文档翻译

State 是当 Widget 被渲染或者在其生命周期中状态改变时,能同步读到相关信息的对象。当实例StatefulWidget 时(下文说的 Widget 无特别说明,都是StatefulWidget 懒得打),必须保证能正确使用 State.setState 来告知该 Widget的状态发生了变化。

当渲染这个 Widget 并将其插入View 树中,会调用 StatefulWidget.createState 然后,framework 就会创建一个 State 对象。 因为一个 Widget 可以被 inflated 多次,比如说 这个 FavoriteWidget 可以被多个地方用到,所以这里会存在多个 _FavoriteWidgetState 与之绑定。同样,当 Widget 被移除出 Views 树的时候,然后又重新插入,这时候 framework 会再次调用StatefulWidget.createState 来创建一个新的 State 对象。
State 的生命周期如下:

名称 返回值/类型 意义
context read-only BuildContext The location in the tree where this widget builds.
mounted read-only bool Whether this State object is currently in a tree.
widget read-only T The current configuration.
hashCode read-only, inherited int The hash code for this object.
runtimeType read-only, inherited Type A representation of the runtime type of the object.
build(BuildContext context) Widget Describes the part of the user interface represented by this widget.
deactivate() void Called when this object is removed from the tree.
debugFillProperties(DiagnosticPropertiesBuilder properties) void Add additional properties associated with the node.
didChangeDependencies() void Called when a dependency of this State object changes.
didUpdateWidget(T oldWidget) void Called whenever the widget configuration changes.
dispose() void Called when this object is removed from the tree permanently.
initState() void Called when this object is inserted into the tree.
reassemble() void Called whenever the application is reassembled during debugging, for example during hot reload.
setState (VoidCallback fn) void Notify the framework that the internal state of this object has changed.

参考资料:
https://docs.flutter.io/flutter/widgets/State-class.html
https://segmentfault.com/a/1190000015211309 推举

上一篇下一篇

猜你喜欢

热点阅读