Flutter:更新简历时的小部件吗?

Flutter:更新简历时的小部件吗?,第1张

Flutter:更新简历时的小部件吗?

您可以通过执行以下 *** 作来监听生命周期事件:

import 'package:flutter/material.dart';import 'package:flutter/foundation.dart';class LifecycleEventHandler extends WidgetsBindingObserver {  final AsyncCallback resumeCallBack;  final AsyncCallback suspendingCallBack;  LifecycleEventHandler({    this.resumeCallBack,    this.suspendingCallBack,  });  @override  Future<Null> didChangeAppLifecycleState(AppLifecycleState state) async {    switch (state) {      case AppLifecycleState.resumed:        if (resumeCallBack != null) {          await resumeCallBack();        }        break;      case AppLifecycleState.inactive:      case AppLifecycleState.paused:      case AppLifecycleState.detached:        if (suspendingCallBack != null) {          await suspendingCallBack();        }        break;    }  }}class AppWidgetState extends State<AppWidget> {  void initState() {    super.initState();    WidgetsBinding.instance.addObserver(      LifecycleEventHandler(resumeCallBack: () async => setState(() {        // do something      }))    );  }  ...}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5013557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存