Note: not until the last moment, do not give up, whether the outcome is successful or not, as long as you have struggled, tried to, all have a clear conscience


In the Flutter application, the lifecycle involves two things: the Widget lifecycle and the application lifecycle. This article describes how to implement Flutter lifecycle monitoring using the flutter_LIFE_state dependency library, similar to Android Activity lifecycle monitoring, similar to iOS UIViewController lifecycle monitoring.

1 Adding a Dependency

Check out the latest version of the Flutter_LIFE_state PUB repository

dependencies:
  flutter_life_state: ^1.0. 0
Copy the code

You can also view the github source for Flutter and reference it with Git as follows:

dependencies:
	shake_animation_widget:
	      git:
	        url: https://github.com/zhaolongs/flutter_life_state.git

Copy the code

Then add lifeFouteObserver to the human entrance:


import 'package:flutter_life_state/flutter_life_state.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    returnMaterialApp( ... navigatorObservers: [ lifeFouteObserver ], ); }}Copy the code

Then the package in the Widget page used is as follows:

import 'package:flutter_life_state/flutter_life_state.dart';
Copy the code

2 Listen for the Widget page lifecycle

To use Flutter_LIFE_state to listen for the Widget’s life cycle, you need to inherit BaseLifeState from the corresponding State of your StatefulWidget, as shown in Listing 2-1:

/// Listing 2-1
class TestPage extends StatefulWidget {
  @override
  _TestPageState createState(a) => _TestPageState();
}

class _TestPageState extends BaseLifeState<TestPage> {
  @override
  Widget build(BuildContext context) {... }}Copy the code

When TestPage shown in Listing 2-1 above is opened using navigator.push, the BaseLifeState lifecycle is called as shown below:Of course, if you need to handle your business needs in the life cycle, just copy the parent BaseLifeState’s life cycle function, as shown in Listing 2-2:

/// Listing 2-2
class TestPage extends StatefulWidget {
  @override
  _TestPageState createState(a) => _TestPageState();
}

class _TestPageState extends BaseLifeState<TestPage> {
  @override
  Widget build(BuildContext context) {... }@override
  void onCreat(a) {
    super.onCreat();
    print("A page onCreat");
  }

  @override
  void onDestory(a) {
    super.onDestory();
    print("A page onDestory"); }}Copy the code

3 BaseLifeState Lifecycle Overview

The BaseLifeState lifecycle callback fully implements the lifecycle of the Android Activity and iOS UIViewController, as shown in Listing 3-1 below.

// listing 3-1
class StateLiveState{
  /// callback when the page is about to be created
  void onWillCreat(a){}
  /// The page creation callback has been created
  void onCreat(a){}
  /// callback when the page is visible
  void onStart(a) {}
  /// callback when the page gets focus
  void onResumed(a) {}
  /// callback when the page loses focus
  void onPause(a) {}
  /// callback when the page is not visible
  void onStop(a) {}
  Callback when the page is about to be destroyed
  void onWillDestory(a){}
  /// page destruction
  void onDestory(a){}}Copy the code

The Widget’s full lifetime occurs between the [onCreate] call and the [onDestroy] call:

  • The onWillCreat method normally initializes some data in this method, where State and BuildContext are not yet bound, so you can’t use context, Data that is normally initialized in the initState method of [State] can be manipulated in the method callback.
  • The onCreate method calls back when the page is visible, when the first frame of the page is drawn, similar to the onCreat method in Android, similar to the viewDidLoad method in iOS for ViewController.
  • The onStart method is called back after [onWillCreat], similar to the onStart method of an Activity on Android, similar to the viewWillAppear method of a ViewController on iOS.
  • The onResumed method is executed after the [onStart] method, which is a callback if the page is in focus, similar to the onResumed method of an Android Activity, similar to the viewDidAppear method of a ViewController in iOS.
  • OnPause isa callback to a page that loses focus, similar to the onPause method of an Android Activity and viewWillDisappear method of a ViewController in iOS.
  • The onStop method isa callback when the page is not visible, similar to the Android Activity onStop method, similar to the iOS ViewController viewDidDisappear method.
  • The onWillDestory method can be used to call the context when the page is about to be destroyed. This is where untying is usually done and only called once.
  • The onDestory method calls back when the page is destroyed that the context has been unbound and cannot be used, similar to the onDestory of the Android Activity and similar to the dealloc of the iOS ViewController.

4 Life Cycle Application Scenarios

4.1 Pages A – B – A

4.2 the Dialog box