当前位置:网站首页>[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 )
边栏推荐
- 一周生活
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
- Three chess games
- 20220702 how do programmers build knowledge systems?
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
- 【剑指 Offer】56 - I. 数组中数字出现的次数
- VictoriaMetrics 简介
- Error in PIP installation WHL file: error: is not a supported wheel on this platform
- A week's life
猜你喜欢

Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
![[shutter] shutter layout component (physicalmodel component)](/img/6a/f8161fb7c8e9012456622f1920da64.gif)
[shutter] shutter layout component (physicalmodel component)

Technical solution of vision and manipulator calibration system

MySQL learning record (7)

PIP audit: a powerful security vulnerability scanning tool

Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file

What is it that makes you tremble? Those without fans can learn

Browser - clean up the cache of JS in the page

Reading experience of just because

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
随机推荐
Gee: (II) resampling the image
MySQL learning record (8)
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
[shutter] shutter layout component (wrap component | expanded component)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
一周生活
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
MySQL learning record (1)
Record the functions of sharing web pages on wechat, QQ and Weibo
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Redis分布式锁故障,我忍不住想爆粗...
[leetcode] sword finger offer 04 Search in two-dimensional array
Lightgbm principle and its application in astronomical data
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
地理探测器原理介绍
PIP audit: a powerful security vulnerability scanning tool
How to prevent your jar from being decompiled?
Three chess games
【C 题集】of Ⅴ