flutter widget 的生命周期监测

flutter ViewWillAppear

当需要和ios开发使用页面的生命周期时,flutter 并没有viewwillappear等方法或者相似的状态管理

//添加监测
WidgetsBinding.instance.addObserver(this);
@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.resumed:
        // TODO: Handle this case.
        break;
      case AppLifecycleState.inactive:
        // TODO: Handle this case.
        break;
      case AppLifecycleState.paused:
        // TODO: Handle this case.
        break;
      case AppLifecycleState.detached:
        // TODO: Handle this case.
        break;
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

你可能感兴趣的:(flutter widget 的生命周期监测)