lifecycle_state 0.0.1

  • Readme
  • Changelog
  • Installing
  • 57

lifecycle_state #

A flutter package similar to Android's lifecycle.
给flutter添加了类似于Android生命周期的方法,其中LifecycleState适用于普通页面,只要把我们用的State替换成LifecycleState即可,LifecycleInnerState适用于PageView中的页面

Getting Started #

导入lifecycle_state

lifecycle_state:
    git:
      url: "https://github.com/tianyu704/lifecycle_state.git"

MaterialApp中添加navigatorObservers: [routeObserver]

import 'package:lifecycle_state/lifecycle_state.dart';

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      navigatorObservers: [routeObserver],
      ...
    );
  }

routeObserver 是在lifecycle_state中定义的

页面中的使用 #

  1. 直接继承LifecycleState
class TestPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TestPageState();
  }
}

class _TestPageState extends LifecycleState<TestPage> {
  PageController _controller;

  @override
  void onCreate() {
    // TODO: implement onCreate
    super.onCreate();
    _controller = PageController();
//    log("onCreate");
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement buildWidget
    return Scaffold(
      body: PageView(
        controller: _controller,
        onPageChanged: (index) {
          eventBus.fire(LifecycleInnerEvent(index, "tag"));
        },
        children: <Widget>[
          ItemPage(0, "tag"),
          ItemPage(1, "tag"),
          ItemPage(2, "tag"),
        ],
      ),
      floatingActionButton: FloatingActionButton(onPressed: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return SecondPage();
        }));
      }),
    );
  }

  @override
  void onResume() {
    // TODO: implement onResume
    super.onResume();
    log("onResume");
  }

  @override
  void onPause() {
    // TODO: implement onPause
    super.onPause();
    log("onPause");
  }

  @override
  void onDestroy() {
    // TODO: implement onDestroy
    super.onDestroy();
    log("onDestroy");
  }

  @override
  void onLoadData() {
    // TODO: implement onLoadData
    super.onLoadData();
    log("onLoadData");
  }

  @override
  void onBackground() {
    // TODO: implement onBackground
    super.onBackground();
    log("onBackground");
  }

  @override
  void onForeground() {
    // TODO: implement onForeground
    super.onForeground();
    log("onForeground");
  }
}
  1. 如果要在PageView中使用,flutter默认每次切换page,旧的page是销毁的,如果你的页面可以这样就可以直接用LifecycleState,但如果你不想每次都销毁重建,就用LifecycleInnerState,但这个稍微麻烦的是得手动通知页面状态变化,这里暂时使用event_bus通信,就是在PageView的onPageChanged中导包并调用eventBus.fire(LifecycleInnerEvent(index, "tag"));,"tag"是用来标记每个pageview的,避免一个pageview改变,通知到别的pageview。LifecycleInnerState中加入了AutomaticKeepAliveClientMixin防止页面销毁,你自己的类中就不要加了
import 'package:schulte_grid_flutter/src/lifecycle_inner_state.dart';
...
PageView(
        controller: _controller,
        onPageChanged: (index) {
          eventBus.fire(LifecycleInnerEvent(index, "tag"));
        },
        children: <Widget>[
          ItemPage(0),
          ItemPage(1),
          ItemPage(2),
        ],
      ),
...
class ItemPage extends StatefulWidget {
  final int index;
  ItemPage(this.index);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ItemPageState();
  }
}

class _ItemPageState extends LifecycleInnerState<ItemPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Text("${widget.index}"),
      ),
    );
  }

  @override
  int setPosition() {
    // TODO: implement setPosition
    return widget.index;
  }

  @override
  void onLoadData() {
    // TODO: implement onLoadData
    super.onLoadData();
    log("onLoadData");
  }

  @override
  void onResume() {
    // TODO: implement onResume
    super.onResume();
    log("onResume");
  }

  @override
  void onPause() {
    // TODO: implement onPause
    super.onPause();
    log("onPause");
  }

  @override
  void onDestroy() {
    // TODO: implement onDestroy
    super.onDestroy();
    log("onDestroy");
  }

  @override
  void onCreate() {
    // TODO: implement onCreate
    super.onCreate();
    log("onCreate");
  }

  @override
  // TODO: implement wa
  //  ntKeepAlive
  bool get wantKeepAlive => true;

  @override
  String setTag() {
    // TODO: implement setTag
    return "tag";
  }
}

参考:

  1. LifecycleState参考于 https://github.com/tinyvampirepudge/flutter_lifecycle_state ,但是他的这个我用着是有bug的,当跳转到继承原生State的页面中再回来时,方法调用会出错,有的生命周期方法会不调用了
  2. LifecycleInnerState参考于 https://github.com/385841539/flutter_BaseWidget 中的一部分,他这个有一个通用的页面生命周期的基类,采用一个map记录了所有页面,然后通过判断当前页面是栈顶或第二个来调用相应的生命周期方法,因此你必须所有页面都继承他的基类,如果跳转到不继承基类的页面中那么那些方法调用就是错误的

综合这两个的优缺点自己改了一个版本,在想用的时候就用LifecycleStatefecycleInnerState,不想用也没关系,不影响用的页面

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

Use this package as a library

1. Depend on it

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


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

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and lifecycle_state.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

The package description is too short. (-6 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

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