当前位置:网站首页>[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 )
边栏推荐
- Introduction to the principle of geographical detector
- MySQL learning record (1)
- Unity3D学习笔记4——创建Mesh高级接口
- [use of pointer and pointer and array]
- From personal heroes to versatile developers, the era of programmer 3.0 is coming
- 在beforeDestroy中销毁localStorage中的值无效
- 2019 Nanchang (relive the classic)
- Detailed explanation of OSI seven layer model
- pyqt图片解码 编码后加载图片
- [shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
猜你喜欢

Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision

MySQL learning record (2)

What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together

Off chip ADC commissioning record

GEE:(二)对影像进行重采样

C language, to achieve three chess games

《Just because》阅读感受

Analysis of neural network
![[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)](/img/5f/e96baefd9481c496024fed345e31fe.jpg)
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)

MySQL learning record (5)
随机推荐
Web side defense Guide
攻防世界pwn题:Recho
Introduction to victoriametrics
APP页面分享口令Rails实现
LightGBM原理及天文数据中的应用
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
20220702 how do programmers build knowledge systems?
Analysis of neural network
PIP audit: a powerful security vulnerability scanning tool
【零基础一】Navicat下载链接
Browser - clean up the cache of JS in the page
App page sharing password rails implementation
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
《Just because》阅读感受
Physical layer cables and equipment
: last child does not take effect
MySQL learning record (5)
pyqt图片解码 编码后加载图片
Evolution of messaging and streaming systems under the native tide of open source cloud
MySQL learning record (6)