Flutter Widget 生命周期

与应用程序的生命周期一样,Flutter 中的小部件也有与之相关的生命周期方法。在此存储库中,我们将研究 Flutter 中的 Widget 生命周期方法。Flutter 中的一切都是一个小部件,因此在考虑生命周期之前,我们应该先考虑 Flutter 中的小部件。
Flutter 主要有两种类型的小部件

1. Stateless Widgets
2. Stateful Widgets

Widget 生命周期阶段

Flutter 中的小部件具有以下相关的生命周期方法

  1. createState

This method is called when we create another Stateful Widget. It is an obligatory strategy.
The createState() returns a case of a State-related with it.
  1. initState

This method is called when this object is inserted into the tree.This will be called exactly
once per State object. Here we can initialize our variables, objects, streams,
AnimationController, and so on.
  1. build

This method is called every time the widget is rebuilt. This can be after call to initState(),
didChangeDependencies(), didUpdateWidget() or when the state is changed via call to setState.
  1. didChangeDependencies

This method is called immediately after initState() and when dependency of the State object
changes via InheritedWidget.
  1. didUpdateWidget

This method is called whenever the widget configuration changes. A typical case is when a parent
passes some variable to the children widget via the constructor.
  1. setState

The setState() method illuminates the framework that the internal state of this item has changed
in a manner that may affect the UI which makes the structure plan a build for this State of the
object.
  1. deactivate
This method is called when the object is removed from the tree.
  1. dispose

This method is called when this object is removed from the tree permanently. Here we should
release any resources retained by this object like stopping animation for instance. One example
when this method is called is while using the pushReplacement() of the Navigator to replace
the current widget with a new one.

视频

有关 Widget 生命周期更详细的信息,请观看此视频
YouTube

GitHub

查看 Github