state_lifecycle 1.0.0 copy "state_lifecycle: ^1.0.0" to clipboard
state_lifecycle: ^1.0.0 copied to clipboard

Page life cycle, the listening page is covered by other pages or is in the state of being displayed

state_lifecycle #

  • 监听页面被其他页面覆盖状态(onPause)或者页面正在展示中的状态(onReTop),不包含 app 按下home键切换到后台的监听
  • The monitoring page is covered by other pages (onPause) or the page is being displayed (onReTop), excluding the app. Press the home button to switch to background monitoring

Supported Platforms

  • Android
  • iOS

How to Use #

# add this line to your dependencies
state_lifecycle: ^1.0.0
import 'package:state_lifecycle/state_lifecycle.dart';

Idea: Monitor route status via navigatorObservers, and obtain route names via didPop and didPush in navigatorObservers to monitor page status #

Step 1: #

在启动app的 MyApp 中 设置 navigatorObservers 正确填写StateNavigatorObserver("MyHomePage")中的路由名称可以使根页面监听到页面状态,填写错误只会影响根页面无法监听页面状态,并不会影响其他页面


class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorObservers: [
        /// example => StateNavigatorObserver("MyHomePage"),
        StateNavigatorObserver("your home page name"),
      ],
      
      home: MyHomePage(title: ''),
    );
  }
}

  • 没有使用onGenerateRoute就无需关心此处

当使用onGenerateRoute时,配置的initialRoute: "MyHomePage" 要和 StateNavigatorObserver("MyHomePage")中的路由名称一致 填写错误只会影响根页面无法监听页面状态,并不会影响其他页面


       // => Navigator.pushNamed(context, "APage");
       
       //或者使用 onGenerateRoute
        onGenerateRoute: (RouteSettings settings) {
          if (settings.name == "MyHomePage") {
            // 如果想要页面跳转动画请参照RouterManager.normalRoute 返回动画路由即可
            return RouterManager.normalRoute(MyHomePage(
              title: "Flutter Demo Home Page",
            ));
          } else if (settings.name == "APage") {
            return RouterManager.normalRoute(APage());
          }
          return null;
        },
        initialRoute: "MyHomePage",

Step 2: #

打开新页面

注意:打开方式错误将无法监听到页面的状态 #


//使用此方法打开新页面,如果想要页面跳转动画请参照RouterManager.normalRoute 返回动画路由即可
Navigator.push(context, RouterManager.normalRoute(APage()));

//或者 使用此方法打开新页面
RouterManager.push(context, APage());

//或者 此方式需要配置 routes:{}
Navigator.pushNamed(context, "APage");

Step 3: #

在需要监听状态的state中with LifecycleMixin

class APageState extends State<APage> with LifecycleMixin{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text("我是A页面")),
      ),
    );
  }

  @override
  void onPause(String routerName) {
    print(routerName + " 暂停状态");
  }

  @override
  void onReTop(String routerName) {
    print(routerName + ' 正在显示状态');
  }
}
0
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Page life cycle, the listening page is covered by other pages or is in the state of being displayed

Repository (GitHub)
View/report issues

License

Apache-2.0 (LICENSE)

Dependencies

flutter

More

Packages that depend on state_lifecycle