flutter 模仿原生的生命周期

189 阅读1分钟

flutter 模仿原生的生命周期

插件地址

pub.flutter-io.cn/packages/hh…

使用方法:

class _Page1State extends LifeCycleBaseState<Page2> {
​
  @override
  Widget onBuild(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: const Text("Page2"),
      ),
      body:  Center(
        child: InkWell(
          onTap: (){
            Get.to(Page2(),preventDuplicates: false);
          },
          child: const Text(
              "Page2"
          ),
        ),
      ),
    );
  }
​
  @override
  void onStart() {
    // TODO: implement onStart
    super.onStart();
  }
​
  @override
  void onDestory() {
    // TODO: implement onDestory
    super.onDestory();
  }
​
  @override
  void onPause() {
    // TODO: implement onPause
    super.onPause();
  }
​
  @override
  void onResume() {
    // TODO: implement onResume
    super.onResume();
  }
}

已知问题

存在不继承LifeCycleBaseState的widget,onPause onResume也会走,如果有页面不继承请自行判断当前路由

原理:

使用了didChangeAppLifecycleState和visibility_detector