Flutter

Flutter Widget 生命周期

2020-05-07  本文已影响0人  minhelloworld

前言

作为一名客户端开发,无论是Android/IOS,生命周期的概念深入骨髓,那么一个优秀的跨平台方案,Flutter Widget是怎么处理生命周期的呢,接下来就让我们一起了解一下,以图镇楼。

Flutter Widget 生命周期

从图中我们可以看到Flutter Widget的生命周期分为两部分:一部分是Widget本身的周期,跟随Widget在layer tree的状态而改变,另一部分则是依赖于Native Application的生命周期。

实践·验证

接下来我们通过实践来验证一下真实的生命周期是否如图中所示,以Android为例:

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    print('initState');
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        print('AppLifecycleState.inactive');
        break;
      case AppLifecycleState.paused:
        print('AppLifecycleState.paused');
        break;
      case AppLifecycleState.resumed:
        print('AppLifecycleState.resumed');
        break;
      case AppLifecycleState.detached:
        print('AppLifecycleState.detached');
        break;
    }

    super.didChangeAppLifecycleState(state);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('didChangeDependencies');
  }

  @override
  void didUpdateWidget(MyHomePage oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('didUpdateWidget');
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    // TODO: implement build
    return MaterialApp(
      home: Center(
          child: GestureDetector(
        child: new Text('lifeCycle'),
        onTap: () {
          Navigator.of(context)
              .push(new MaterialPageRoute(builder: (BuildContext c) {
            return new Text('sdfs');
          }));
        },
      )),
    );
  }

  @override
  void reassemble() {
    super.reassemble();
    print('reassemble');
  }

  @override
  void deactivate() {
    super.deactivate();
    print('deactivate');
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.addObserver(this);
    print('dispose');
  }
}

1、创建一个wedget到显示 打印结果如下

01-15 14:06:44.751 19005-19025/com.example.flutterapp I/flutter: initState
01-15 14:06:44.751 19005-19025/com.example.flutterapp I/flutter: didChangeDependencies
01-15 14:06:44.752 19005-19025/com.example.flutterapp I/flutter: build

2、跳转到其他页面执行

01-15 14:08:40.956 19005-19561/com.example.flutterapp I/flutter: deactivate
01-15 14:08:40.970 19005-19561/com.example.flutterapp I/flutter: dispose

3、点击热重载按钮

01-15 14:09:55.467 19005-19561/com.example.flutterapp I/flutter: reassemble
01-15 14:09:55.476 19005-19561/com.example.flutterapp I/flutter: didUpdateWidget
01-15 14:09:55.476 19005-19561/com.example.flutterapp I/flutter: build

4、app由显示切换到后台(home状态)

01-15 14:11:10.062 19005-19561/com.example.flutterapp I/flutter: AppLifecycleState.inactive
01-15 14:11:10.524 19005-19561/com.example.flutterapp I/flutter: AppLifecycleState.paused

5、app由后台切回前台

01-15 14:11:54.836 19005-19561/com.example.flutterapp I/flutter: AppLifecycleState.inactive
01-15 14:11:54.837 19005-19561/com.example.flutterapp I/flutter: AppLifecycleState.resumed

代码中的didChangeAppLifecycleState方法需要复写WidgetsBindingObserver这个抽象类

参考

上一篇下一篇

猜你喜欢

热点阅读