美文网首页
Flutter 生命周期

Flutter 生命周期

作者: Passon_Fang | 来源:发表于2019-06-26 14:11 被阅读0次

StatefulWidget 生命周期

createState
initState
didChangeDependencies
build
(didUpdateWidget)

deactivate
dispose

App 生命周期

使用 WidgetsBindingObserver 监听

class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver

注册监听/解除监听
initState: WidgetsBinding.instance.addObserver(this);
dispose: WidgetsBinding.instance.removeObserver(this);

//回调
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  super.didChangeAppLifecycleState(state);
  if (state == AppLifecycleState.paused) {
    // went to Background
  }
  if (state == AppLifecycleState.resumed) {
    // came back to Foreground
  }
}

相关文章

网友评论

      本文标题:Flutter 生命周期

      本文链接:https://www.haomeiwen.com/subject/vqrccctx.html