page_life_cycle 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

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)

   }

[1.1.1] - TODO: 2019-12-24. #

  • TODO: fix onAppBackground.

[1.1.0] - TODO: Add release date. #

  • TODO: Remove manual add listening.

[1.0.8] - TODO: Add release date. #

  • TODO: support no route name.

[1.0.6] - TODO: Add release date. #

  • TODO: Describe initial release.

[1.0.2] - fix readme.md. #

  • fix readme.md.

[1.0.1] - TODO: Add release date. #

  • TODO: Describe initial release.

[1.0.0] - fix readme.md. #

  • fix readme.md.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:page_life_cycle/page_life_cycle.dart';
import 'route.dart';
import 'package:fluro/fluro.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {} on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    final router = new Router();
    Routes.configureRoutes(router);
    return MaterialApp(
      onGenerateRoute: Routes.router.generator,
      navigatorObservers: [PageNavigatorObserver()],
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  page_life_cycle: ^1.1.1

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:page_life_cycle/page_life_cycle.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
55
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on Apr 7, 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

Fix lib/page_life_cycle.dart. (-1 points)

Analysis of lib/page_life_cycle.dart reported 2 hints:

line 12 col 28: The value of the field '_appLifecycleState' isn't used.

line 139 col 12: The value of the local variable 'name' isn't used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.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