当前位置:网站首页>[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
2022-07-02 22:14:00 【Programmer community】
List of articles
- One 、Flutter Page lifecycle
- 1、StatelessWidget Component lifecycle function
- 2、StatefulWidget Component lifecycle function
- Two 、StatefulWidget Component lifecycle
- 1、createState
- 2、initState
- 3、didChangeDependencies
- 4、build
- 5、didUpdateWidget
- 6、deactivate
- 7、dispose
- 3、 ... and 、 Complete code example
- Four 、 Related resources
One 、Flutter Page lifecycle
Flutter The page lifecycle is Flutter Page components Widget Life cycle of ;
The components of the page are StatefulWidget Components and StatelessWidget Components ;
1、StatelessWidget Component lifecycle function
StatelessWidget Component lifecycle function : Only two. , Namely createElement() , build() Two methods ;
abstract class StatelessWidget extends Widget {
/// Initializes [key] for subclasses. const StatelessWidget({
Key key }) : super(key: key); @override StatelessElement createElement() => StatelessElement(this); @protected Widget build(BuildContext context);}2、StatefulWidget Component lifecycle function
StatefulWidget The component lifecycle is divided into three groups :
① Initialization period : createState , initState ;
② Update period : didChangeDependencies , build , didUpdateWidget ;
③ Destruction period : deactivate , dispose ;
Two 、StatefulWidget Component lifecycle
1、createState
createState function :
- Period : Initialization life cycle function
- Timing of invocation : establish StatefulWidget The first method after that is called. ;
- Abstract method : This method is an abstract method , This method must be overridden ;
/// 1. Initialization life cycle function /// establish StatefulWidget The first method after that is called. , /// This method is an abstract method , Must cover @override _WidgetLiftCyclePageState createState() => _WidgetLiftCyclePageState();2、initState
initState function :
- Period : Initialization life cycle function
- Timing of invocation : The method is to create Widget Component is the first method besides the construction method ,
- Corresponding method : Corresponding Android Medium onCreate Method ; Corresponding iOS Medium viewDidLoad Method ;
- Common usage : Perform some initialization operations in this method ;
/// 2. Initialization life cycle function /// The method is to create Widget Component is the first method besides the construction method /// This method corresponds to Android Medium onCreate Method /// Corresponding iOS Medium viewDidLoad Method /// Common usage : Perform some initialization operations in this method @override void initState() {
print("initState"); super.initState(); }3、didChangeDependencies
didChangeDependencies function :
- Period : Life cycle function of update period ;
- Timing of invocation : ① establish Widget When the component , Call complete initState After the method , Call the method ; ② InheritedWidget relevant ( Temporary does not involve ) ;
/// 3. Life cycle function of update period /// Method call timing : /// ① establish Widget When the component , Call complete initState After the method , Call the method /// ② InheritedWidget relevant ( Temporary does not involve ) @override void didChangeDependencies() {
/// The method must call the method of the parent class at the beginning super.didChangeDependencies(); print("didChangeDependencies"); }4、build
build function :
- Period : Life cycle function of update period ;
- Timing of invocation : ① Call complete didChangeDependencies Method is called ; ② call setState After method , This method will also be called ;
- Method function : This method is called every time the page is rendered ;
/// 4. Life cycle function of update period /// Method call timing : /// ① Call complete didChangeDependencies Method is called /// ② call setState After method , This method will also be called ; /// This method is called every time the page is rendered @override Widget build(BuildContext context) {
print("build"); return Scaffold( appBar: AppBar( // title title: Text("StatefulWidget Page lifecycle "), // Back off button leading: BackButton(), ), body: Center( child: Column( children: <Widget>[ RaisedButton( // Click event onPressed: (){
// Callback update period Life cycle function setState(() {
}); }, child: Text(" call setState() Method "), ), ], ), ), ); }5、didUpdateWidget
didUpdateWidget function :
- Period : Life cycle function of update period ;
- Timing of invocation : This lifecycle method is not often called , This method is called only when the parent container component is redrawn ;
- Methods and mechanisms : Incoming oldWidget Parameters are old component information , Change the current Widget With the old Widget Contrast , If it's not equal , Then perform additional operations ;
/// 5. Life cycle function of update period /// Method call timing : This lifecycle method is not often called , This method is called only when the parent container component is redrawn /// Methods and mechanisms : Incoming oldWidget Parameters are old component information , /// Change the current Widget With the old Widget Contrast , If it's not equal , Then perform additional operations /// Such as : The properties of components are not equal , The values of the fields are not equal @override void didUpdateWidget(WidgetLiftCyclePage oldWidget) {
super.didUpdateWidget(oldWidget); print("didUpdateWidget"); }6、deactivate
deactivate function :
- Period : Life cycle function of destruction period ;
- Timing of invocation : This lifecycle method is not often called , Call only when the component is removed ;
/// 6 . Life cycle function of destruction period /// Method call timing : This lifecycle method is not often called , Call only when the component is removed /// The method in dispose Method was previously called @override void deactivate() {
super.deactivate(); print("deactivate"); }7、dispose
dispose function :
- Period : Life cycle function of destruction period ;
- Timing of invocation : Called when a component is destroyed , To release and destroy resources in this method ;
/// 7 . Life cycle function of destruction period /// Method call timing : Called when a component is destroyed , To release and destroy resources in this method @override void dispose() {
super.dispose(); print("dispose"); }3、 ... and 、 Complete code example
Complete code example :
import 'package:flutter/material.dart';class WidgetLiftCyclePage extends StatefulWidget {
/// 1. Initialization life cycle function /// establish StatefulWidget The first method after that is called. , /// This method is an abstract method , Must cover @override _WidgetLiftCyclePageState createState() => _WidgetLiftCyclePageState();}class _WidgetLiftCyclePageState extends State<WidgetLiftCyclePage> {
/// 2. Initialization life cycle function /// The method is to create Widget Component is the first method besides the construction method /// This method corresponds to Android Medium onCreate Method /// Corresponding iOS Medium viewDidLoad Method /// Common usage : Perform some initialization operations in this method @override void initState() {
print("initState"); super.initState(); } /// 3. Life cycle function of update period /// Method call timing : /// ① establish Widget When the component , Call complete initState After the method , Call the method /// ② InheritedWidget relevant ( Temporary does not involve ) @override void didChangeDependencies() {
/// The method must call the method of the parent class at the beginning super.didChangeDependencies(); print("didChangeDependencies"); } /// 4. Life cycle function of update period /// Method call timing : /// ① Call complete didChangeDependencies Method is called /// ② call setState After method , This method will also be called ; /// This method is called every time the page is rendered @override Widget build(BuildContext context) {
print("build"); return Scaffold( appBar: AppBar( // title title: Text("StatefulWidget Page lifecycle "), // Back off button leading: BackButton(), ), body: Center( child: Column( children: <Widget>[ RaisedButton( // Click event onPressed: (){
// Callback update period Life cycle function setState(() {
}); }, child: Text(" call setState() Method "), ), ], ), ), ); } /// 5. Life cycle function of update period /// Method call timing : This lifecycle method is not often called , This method is called only when the parent container component is redrawn /// Methods and mechanisms : Incoming oldWidget Parameters are old component information , /// Change the current Widget With the old Widget Contrast , If it's not equal , Then perform additional operations /// Such as : The properties of components are not equal , The values of the fields are not equal @override void didUpdateWidget(WidgetLiftCyclePage oldWidget) {
super.didUpdateWidget(oldWidget); print("didUpdateWidget"); } /// 6 . Life cycle function of destruction period /// Method call timing : This lifecycle method is not often called , Call only when the component is removed /// The method in dispose Method was previously called @override void deactivate() {
super.deactivate(); print("deactivate"); } /// 7 . Life cycle function of destruction period /// Method call timing : Called when a component is destroyed , To release and destroy resources in this method @override void dispose() {
super.dispose(); print("dispose"); }}Running effect :

Print log : Enter interface , Click on
3
3
3 Next button , Then exit the interface ;
- Enter interface : First call initState Method , And then call didChangeDependencies Method , Last call build Method ;
I/flutter (21393): initStateI/flutter (21393): didChangeDependenciesI/flutter (21393): build- Click button : Print once every time you click the button build , A total of
3
3
3 Time build Method ;
I/flutter (21393): buildI/flutter (21393): buildI/flutter (21393): build- Exit the screen : First call deactivate Method , Last call dispose Method ;
I/flutter (21393): deactivateI/flutter (21393): dispose- Full log :
Performing hot reload...Syncing files to device Pixel 2...Reloaded 2 of 489 libraries in 510ms.I/flutter (21393): initStateI/flutter (21393): didChangeDependenciesI/flutter (21393): buildI/flutter (21393): buildI/flutter (21393): buildI/flutter (21393): buildI/flutter (21393): deactivateI/flutter (21393): disposeFour 、 Related resources
Reference material :
- Flutter Official website : https://flutter.dev/
- Flutter Plug in download address : https://pub.dev/packages
- Flutter Developing documents : https://flutter.cn/docs ( Strongly recommend )
- official GitHub Address : https://github.com/flutter
- Flutter The Chinese community : https://flutter.cn/
- Flutter Practical tutorial : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart Chinese document : https://dart.cn/
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net ( unofficial , The translation is very good ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_cmd ( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : https://download.csdn.net/download/han1202012/15547438 ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
- TinyMCE visual editor adds Baidu map plug-in
- PIP version update timeout - download using domestic image
- Chargement de l'image pyqt après décodage et codage de l'image
- Official announcement! The golden decade of new programmers and developers was officially released
- Web side defense Guide
- Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- Ransack combined condition search implementation
- D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
- MySQL learning record (2)
猜你喜欢

地理探测器原理介绍

Redis distributed lock failure, I can't help but want to burst

分享一下如何制作专业的手绘电子地图

读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...

C language, to achieve three chess games

sql service 截取字符串

Etcd raft protocol
![[zero foundation I] Navicat download link](/img/23/e7808884152eeaf186fe756d6adac6.png)
[zero foundation I] Navicat download link

Daily book - low code you must understand in the era of digital transformation

Off chip ADC commissioning record
随机推荐
在beforeDestroy中销毁localStorage中的值无效
How to prevent your jar from being decompiled?
LandingSite eBand B1冒烟测试用例
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
[sword finger offer] 56 - I. the number of numbers in the array
Summary of the first week of summer vacation
[zero foundation I] Navicat download link
*C language final course design * -- address book management system (complete project + source code + detailed notes)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
From "bronze" to "King", there are three secrets of enterprise digitalization
Lightgbm principle and its application in astronomical data
Try to get property'num for PHP database data reading_ rows' of non-object?
[shutter] shutter layout component (physicalmodel component)
服务可见可观测性
如何访问kubernetes API?
Physical layer cables and equipment
Infrastructure is code: a change is coming
[shutter] shutter page Jump (route | navigator | page close)
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...