Flutter State生命周期 Flutter Widget生命周期 Flutter 应用程序生命周期

红太狼 2022-12-27 08:28 270阅读 0赞

在 Flutter应用程序中,生命周期涉及两个,一个是 Widget 的生命周期,一个是应用程序的生命周期,本文章说明通过 flutter_life_state 依赖库实现在 Flutter 中类似 Android 中Activity的生命周期监听,类似 iOS UIViewController 的生命周期。
在这里插入图片描述

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_life_state/flutter_life_state.dart';
  3. void main() {
  4. runApp(MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Test Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. visualDensity: VisualDensity.adaptivePlatformDensity,
  15. ),
  16. home: MyHomePage(title: 'Flutter Demo Home Page'),
  17. );
  18. }
  19. }
  20. class MyHomePage extends StatefulWidget {
  21. MyHomePage({Key key, this.title}) : super(key: key);
  22. final String title;
  23. @override
  24. _MyHomePageState createState() => _MyHomePageState();
  25. }
  26. class _MyHomePageState extends State<MyHomePage> {
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. appBar: AppBar(
  31. title: Text(widget.title),
  32. ),
  33. body: Center(
  34. child: Column(
  35. mainAxisAlignment: MainAxisAlignment.center,
  36. children: <Widget>[
  37. RaisedButton(
  38. child: Text("测试生命周期"),
  39. onPressed: () {
  40. Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
  41. return new TestPage();
  42. }));
  43. },
  44. )
  45. ],
  46. ),
  47. ),
  48. );
  49. }
  50. }
  51. class TestPage extends StatefulWidget {
  52. @override
  53. _TestPageState createState() => _TestPageState();
  54. }
  55. class _TestPageState extends BaseLifeState<TestPage> {
  56. @override
  57. Widget build(BuildContext context) {
  58. return Scaffold(
  59. appBar: AppBar(
  60. title: Text("A页面"),
  61. ),
  62. body: Center(
  63. child: Column(
  64. mainAxisAlignment: MainAxisAlignment.center,
  65. children: <Widget>[
  66. RaisedButton(
  67. child: Text(" push B页面 "),
  68. onPressed: () {
  69. Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
  70. return new TestBPage();
  71. }));
  72. },
  73. )
  74. ],
  75. ),
  76. ),
  77. );
  78. }
  79. @override
  80. void onWillCreat() {
  81. super.onWillCreat();
  82. print(" A 页面 onWillCreat");
  83. }
  84. @override
  85. void onCreat() {
  86. super.onCreat();
  87. print(" A 页面 onCreat");
  88. }
  89. @override
  90. void onStart() {
  91. super.onStart();
  92. print(" A 页面 onStart");
  93. }
  94. @override
  95. void onResumed() {
  96. super.onResumed();
  97. print(" A 页面 onResumed");
  98. }
  99. @override
  100. void onPause() {
  101. super.onPause();
  102. print(" A 页面 onPause");
  103. }
  104. @override
  105. void onStop() {
  106. super.onStop();
  107. print(" A 页面 onStop");
  108. }
  109. @override
  110. void onWillDestory() {
  111. super.onWillDestory();
  112. print(" A 页面 onWillDestory");
  113. }
  114. @override
  115. void onDestory() {
  116. super.onDestory();
  117. print(" A 页面 onDestory");
  118. }
  119. }
  120. class TestBPage extends StatefulWidget {
  121. @override
  122. _TestBPageState createState() => _TestBPageState();
  123. }
  124. class _TestBPageState extends BaseLifeState<TestBPage> {
  125. @override
  126. Widget build(BuildContext context) {
  127. return Scaffold(
  128. appBar: AppBar(
  129. title: Text("B页面"),
  130. ),
  131. body: Center(
  132. child: Column(
  133. mainAxisAlignment: MainAxisAlignment.center,
  134. children: <Widget>[
  135. RaisedButton(
  136. child: Text("返回A页面 "),
  137. onPressed: () {
  138. Navigator.of(context).pop();
  139. }),
  140. ],
  141. ),
  142. ),
  143. );
  144. }
  145. @override
  146. void onWillCreat() {
  147. super.onWillCreat();
  148. print(" B 页面 onWillCreat");
  149. }
  150. @override
  151. void onCreat() {
  152. super.onCreat();
  153. print(" B 页面 onCreat");
  154. }
  155. @override
  156. void onStart() {
  157. super.onStart();
  158. print(" B 页面 onStart");
  159. }
  160. @override
  161. void onResumed() {
  162. super.onResumed();
  163. print(" B 页面 onResumed");
  164. }
  165. @override
  166. void onPause() {
  167. super.onPause();
  168. print(" B 页面 onPause");
  169. }
  170. @override
  171. void onStop() {
  172. super.onStop();
  173. print(" B 页面 onStop");
  174. }
  175. @override
  176. void onWillDestory() {
  177. super.onWillDestory();
  178. print(" B 页面 onWillDestory");
  179. }
  180. @override
  181. void onDestory() {
  182. super.onDestory();
  183. print(" B 页面 onDestory");
  184. }
  185. }

1 添加依赖

点击查看 flutter_life_state pub仓库 最新版本

  1. dependencies:
  2. flutter_life_state: ^1.0.1
  • 当然你也可以点击查看 github 源码,在Flutter 中也可通过git方式来引用如下:

    dependencies:

    1. shake_animation_widget:
    2. git:
    3. url: https://github.com/zhaolongs/flutter_life_state.git
  • 然后在人的入口添加 lifeFouteObserver :

    import ‘package:flutter_life_state/flutter_life_state.dart’;

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {

    1. return MaterialApp(
    2. ...
    3. navigatorObservers: [
    4. lifeFouteObserver
    5. ],
    6. );

    }
    }

然后在使用到的 Widget 页面中导包如下:

  1. import 'package:flutter_life_state/flutter_life_state.dart';

2 监听 Widget 页面生命周期

使用 flutter_life_state 来监听 Widget 的生命周期时,你需要在你的 StatefulWidget 对应的State 继承 BaseLifeState 如下代码清单 2-1 所示:

  1. ///代码清单 2-1
  2. class TestPage extends StatefulWidget {
  3. @override
  4. _TestPageState createState() => _TestPageState();
  5. }
  6. class _TestPageState extends BaseLifeState<TestPage> {
  7. @override
  8. Widget build(BuildContext context) { ...}
  9. }

当使用 Navigator.push 方式将上述代码清单 2-1 所示的 TestPage 打开时,会调用 BaseLifeState 的生命周期如下图所示:
在这里插入图片描述
当然如果你需要在生命周期中处理你的业务需要,直接复写父类 BaseLifeState 的生命周期函数就好,如下代码清单 2-2所示:

  1. ///代码清单 2-2
  2. class TestPage extends StatefulWidget {
  3. @override
  4. _TestPageState createState() => _TestPageState();
  5. }
  6. class _TestPageState extends BaseLifeState<TestPage> {
  7. @override
  8. Widget build(BuildContext context) {...}
  9. @override
  10. void onCreat() {
  11. super.onCreat();
  12. print(" A 页面 onCreat");
  13. }
  14. @override
  15. void onDestory() {
  16. super.onDestory();
  17. print(" A 页面 onDestory");
  18. }
  19. }

3 BaseLifeState 生命周期概述

BaseLifeState 生命周期回调完全实现了 Android 中Activity、iOS UIViewController 的生命周期,如下源码清单 3-1 所示。

  1. ///源码清单 3-1
  2. class StateLiveState{
  3. ///页面即将创建时回调
  4. void onWillCreat(){}
  5. ///页面创建时的回调 已创建好
  6. void onCreat(){}
  7. ///页面可见时回调
  8. void onStart() {}
  9. ///页面获取焦点时回调
  10. void onResumed() {}
  11. ///页面失去焦点时回调
  12. void onPause() {}
  13. ///页面不可见时回调
  14. void onStop() {}
  15. ///页面即将销毁时回调
  16. void onWillDestory(){}
  17. ///页面销毁
  18. void onDestory(){}
  19. }

Widget 的完整生存期会在 [onCreate]调用和[onDestroy]调用之间发生:

  • onWillCreat 方法 一般在这个方法中初始化一些数据,在此方法中 State与 BuildContext还未绑定好,所以是不可使用context的,就如大家平时在 [State]的initState方法中初始化的数据就可以在这个方法回调中进行操作。
  • onCreate 方法 在页面可见时回调,也就是页面的第一帧绘制完毕后回调,类似Android中的onCreat方法,类似iOS中ViewController的 viewDidLoad 方法。
  • onStart 方法 在[onWillCreat]之后回调, 类似Android的Activity的onStart方法,类似iOS中ViewController的 viewWillAppear 。
  • onResumed 方法在[onStart]方法后执行,页面有焦点时的回调,类似Android的Activity的onResumed方法, 类似iOS中ViewController的viewDidAppear 。
  • onPause 方法在页面失去焦点时的回调, 类似Android的Activity的onPause方法,类似iOS中ViewController的 viewWillDisappear。
  • onStop 方法在页面不可见时的回调,类似Android的Activity的onStop方法,类似iOS中ViewController的 viewDidDisappear。
  • onWillDestory 方法在页面即将销毁时调用 context 可用,一般在这里进行解绑操作,只会调用一次。
  • onDestory方法在页面销毁时回调 context 已解绑 不可使用,类似Android的Activity的onDestory,类似iOS中ViewController的 dealloc。

4 生命周期使用场景

4.1 页面 A - 页面B - 页面 A

在这里插入图片描述

4.2 Dialog 弹框

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读