当前位置:网站首页>[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): dispose
Four 、 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 )
边栏推荐
- A week's life
- Les trois principaux points de douleur traités par servicemesh
- Jar package startup failed -mysql modify the default port number / set password free enter
- 服务可见可观测性
- Using emqx cloud to realize one machine one secret verification of IOT devices
- MySQL learning record (8)
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- Introduction to victoriametrics
- Pyqt picture decodes and encodes and loads pictures
- beginning
猜你喜欢
情感计算与理解研究发展概述
Introduction to the principle of geographical detector
图像基础概念与YUV/RGB深入理解
The neo4j skill tree was officially released to help you easily master the neo4j map database
What is it that makes you tremble? Those without fans can learn
地理探测器原理介绍
How do I access the kubernetes API?
The difference between include < > and include ""
PIP audit: a powerful security vulnerability scanning tool
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
随机推荐
100 important knowledge points that SQL must master: management transaction processing
sql service 截取字符串
【leetcode】1380. Lucky number in matrix
Landingsite eband B1 smoke test case
[use of pointer and pointer and array]
Kubernetes resource object introduction and common commands (4)
关于测试用例
An overview of the development of affective computing and understanding research
【剑指 Offer】57. 和为s的两个数字
MySQL learning record (9)
C language, to achieve three chess games
CVPR论文解读 | 弱监督的高保真服饰模特生成
Jar package startup failed -mysql modify the default port number / set password free enter
The neo4j skill tree was officially released to help you easily master the neo4j map database
Une semaine de vie
Gbase8s database type
ServiceMesh主要解决的三大痛點
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
ServiceMesh主要解决的三大痛点
Error in PIP installation WHL file: error: is not a supported wheel on this platform