5

【老孟Flutter】Flutter 中与平台相关的生命周期

 3 years ago
source link: https://segmentfault.com/a/1190000038834863
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【老孟Flutter】Flutter 中与平台相关的生命周期

发布于 32 分钟前

老孟导读:关于生命周期的文章共有2篇,一篇(此篇)是介绍 Flutter 中Stateful 组件的生命周期。

第二篇是 Flutter 中与平台相关的生命周期,

博客地址:http://laomengit.com/blog/20201227/%E7%9B%B8%E5%85%B3%E5%B9%B3%E5%8F%B0%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F.html

博客中还有更多精彩文章,也欢迎加入 Flutter 交流群。

此篇文章所说的生命周期与 StatefulWidget 组件的生命周期是不同的,这里平台相关的生命周期指的是特定平台相关操作所产生的生命周期,比如 Android 中 App 退到后台后的onPause等。

有人下场景,App正在播放视频,此时回到手机桌面或者切换到其他App,那么此时视频应该暂停播放,Flutter 中使用 AppLifecycleState 实现:

class AppLifecycle extends StatefulWidget {
  @override
  _AppLifecycleState createState() => _AppLifecycleState();
}

class _AppLifecycleState extends State<AppLifecycle>
    with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      //TODO
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('App 生命周期'),
      ),
      body: Center(
        child: Text(''),
      ),
    );
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

重点是重写 didChangeAppLifecycleState 方法,AppLifecycleState 中的状态包括:resumed、inactive、paused、detached。

didChangeAppLifecycleState 方法的回调来源于系统的通知(notifications),正常情况下,App是能正常接收到这些通知,但有的情况下是无法接收到通知的,比如用户强制关机、手机没有电自动关机等。

下面对其状态详细说明:

  • resumed:应用程序可见且响应用户输入。
  • inactive:应用程序处于非激活状态,无法响应用户输入。在iOS上,打电话、响应TouchID请求、进入应用程序切换器或控制中心都处于此状态。在Android上,分屏应用,打电话,弹出系统对话框或其他窗口等。
  • pause:应用程序不可见且无法响应用户输入,运行在后台。处于此状态时,引擎将不会调用 Window.onBeginFrameWindow.onDrawFrame
  • detached:应用程序仍寄存在Flutter引擎上,但与平台 View 分离。处于此状态的时机:引擎首次加载到附加到一个平台 View的过程中,或者由于执行 Navigator pop ,view 被销毁。

下面是关于生命周期经常遇到的问题:

有2个页面A和B,在B页面点击返回键返回到A,didChangeAppLifecycleState 不回调

其实这个问题大部分人是想要实现类似于Android 中 onResume 中的功能,用 didChangeAppLifecycleState 是无法实现此功能的,didChangeAppLifecycleState 是对应于整个应用程序的,而不是 Flutter 中 不同的路由(页面)。

从A->B,在从B返回A,A重新加载数据使用如下方法:

A页面代码:

class A extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(onPressed: ()async{
      var result = await Navigator.of(context).push(MaterialPageRoute(builder: (context){
        return B();
      }));
      //从B返回到A时,执行下面的代码
      //TODO 加载数据
    });
  }
}

B页面代码:

class B extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(onPressed: (){
      Navigator.of(context).pop('返回的参数');
    });
  }
}

老孟Flutter博客(330个控件用法+实战入门系列文章):http://laomengit.com

欢迎加入Flutter交流群(微信:laomengit)、关注公众号【老孟Flutter】:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK