state_lifecycle 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

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 viadidPop and didPush innavigatorObservers 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 + ' 正在显示状态');
  }
}

[1.0.0] #

  • 监听页面被其他页面覆盖状态(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

example/lib/main.dart

import 'package:example/APage.dart';
import 'package:example/BPage.dart';
import 'package:flutter/material.dart';
import 'package:state_lifecycle/state_lifecycle.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      navigatorObservers: [
        StateNavigatorObserver("MyHomePage"),
      ],
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),

//      routes: {
//        "APage": (context) {
//          return APage();
//        },
//        "BPage": (context) {
//          return BPage();
//        },
//        "MyHomePage": (context) {
//          return MyHomePage(title : "11");
//        },
//      },

//      onGenerateRoute: (RouteSettings settings) {
//        if (settings.name == "MyHomePage") {
//          return RouterManager.normalRoute(MyHomePage(
//            title: "Flutter Demo Home Page",
//          ));
//        } else if (settings.name == "APage") {
//          return RouterManager.normalRoute(APage());
//        }
//        return null;
//      },
//      initialRoute: "MyHomePage",
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with LifecycleMixin{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.teal,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
                onPressed: () {
                  Navigator.push(context, RouterManager.normalRoute(APage()));
//                  Navigator.pushNamed(context, "APage");
//                  Navigator.push(context, new MaterialPageRoute(
//                      builder: (context) {
//                        return APage();
//                      },),);
                },
                child: Text("打开APage")),
          ],
        ),
      ),
    );
  }

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

  @override
  void onReTop(String routerName) {
    print(routerName + ' 正在显示状态');
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  state_lifecycle: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:state_lifecycle/state_lifecycle.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/state_lifecycle.dart.

Run flutter format to format lib/state_lifecycle.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test