page_life_cycle 1.1.1

Flutter Android iOS web

Because the widget of the flitter does not route the Onshow and onhide events, the Onshow and onhide events of the widget are added according to the routing changes.

page_life_cycle #

由于flutter本身widget没有路由onShow,onHide事件,根据路由变化增加widget的onShow,onHide事件,同时继承了WidgetsBindingObserver的didChangeAppLifecycleState app周期,同时优化了app进入前后台事件 只触发当前路由是顶层路由的widget,而不是所有的路由

Getting Started #

首次使用 #

   main.dart

   import 'package:page_life_cycle/page_life_cycle.dart' show PageNavigatorObserver;


   MaterialApp(
       navigatorObservers: [PageNavigatorObserver()]
   )

Widget内使用 #

1.1.0版本

   main.dart

   import 'package:page_life_cycle/page_life_cycle.dart';



  class VideoWidgetState extends State<VideoWidget>
    with PageLifeCycle  {
    // 路由显示触发
    @override
    void onShow () {

    }
    
    // 路由隐藏触发
    @override
    void onHide () {

    }
    // app 进入后台,只触发当前路由内绑定的widget
    @override
    void onAppBackground () {

    }
    // app 进入前台,只触发当前路由内绑定的widget
    @override
    void onAppForeground () {

    }
    // 判断是否是顶层路由 会过滤没有name的route 和 modelPopupRoute 需要在addPageLifeCycleObserver方法后调用
    bool getIsTopRoute => PageLifeCycleObserver().getIsTopRoute(this)

   }
+

1.1.0之前版本

   main.dart

   import 'package:page_life_cycle/page_life_cycle.dart';

PageLifeCycleObserver lifeCycle = PageLifeCycleObserver();

  class VideoWidgetState extends State<VideoWidget>
    with PageLifeCycle  {


   @override
   void didChangeDependencies() {
      super.didChangeDependencies();
      lifeCycle.addPageLifeCycleObserver(this, ModalRoute.of(context));
   }

   @override
   void dispose() {
      super.dispose();
      lifeCycle.removePageLifeCycleObserver(this);
   }

    // 路由显示触发
    @override
    void onShow () {

    }
    
    // 路由隐藏触发
    @override
    void onHide () {

    }
    // app 进入后台,只触发当前路由内绑定的widget
    @override
    void onAppBackground () {

    }
    // app 进入前台,只触发当前路由内绑定的widget
    @override
    void onAppForeground () {

    }
    // 判断是否是顶层路由 会过滤没有name的route 和 modelPopupRoute 需要在addPageLifeCycleObserver方法后调用
    bool getIsTopRoute => PageLifeCycleObserver().getIsTopRoute(this)

   }

4
likes
100
pub points
58%
popularity

Because the widget of the flitter does not route the Onshow and onhide events, the Onshow and onhide events of the widget are added according to the routing changes.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

hjcandxsd@gmail.com

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on page_life_cycle