Giter Club home page Giter Club logo

flutterlifecyclehooks's Introduction

flutterlifecyclehooks

A flutter package that lets you easily add hooks for application lifecycle state. LifecycleMixin uses the AppLifecycleListener and registers methods as hooks.

The list of hooks is:

  • onAppShow
  • onAppHide
  • onAppRestart
  • onAppResume
  • onAppPause
  • onAppInactive
  • onAppDetach
  • onExitAppRequest

Note: Read more about these lifecycle hooks https://api.flutter.dev/flutter/widgets/AppLifecycleListener-class.html

Optionally provides onContextReady that acts like initState but BuildContext is available for use.

Getting Started

Add on pubspec.yaml

dependencies:
    flutterlifecyclehooks: version

Example code

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> with LifecycleMixin {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text(
            currentLifecycleState.toString(),
          ),
        ),
      ),
    );
  }

  @override
  void onAppLifecycleChange(AppLifecycleState state) {
    setState(() {});
  }

  @override
  void onResume() {
    print('did resume');
  }

  @override
  void onPause() {
    print('did pause');
  }
}

Also you can use, LifecycleHooksSubscriber wraps a widget and provides convient arguments for lifecycle hooks.

You can use this if you don't want to polute the namespace of a stateful widget.

flutterlifecyclehooks's People

Contributors

spideythewebhead avatar

Stargazers

jimmy avatar truongcvtt avatar  avatar Vadym Parakonnyi avatar Nikos Portolos avatar Superazan avatar AbdulMuaz Aqeel avatar Vinh Bui avatar  avatar Murat Küpeli avatar  avatar

Watchers

 avatar

flutterlifecyclehooks's Issues

Support for null-safety

Will this plugin support null-safety to stay up to date with flutter v2.+?. The code is not that long and should be easy to migrate.

Add onInactive callback

Hey, thanks for this package!
Could you add onInactive callback to the mixin, please? It should be 1 line change ☺️

Type 'AppExitResponse' not found

: Error: Type 'AppExitResponse' not found.
lifecycle_mixin.dart:104
Future didRequestAppExit() async {
^^^^^^^^^^^^^^^
: Error: The getter 'AppExitResponse' isn't defined for the class 'LifecycleMixin'.
lifecycle_mixin.dart:105

  • 'LifecycleMixin' is from 'package:flutterlifecyclehooks/src/lifecycle_mixin.dart' ('../../.pub-cache/hosted/pub.dev/flutterlifecyclehooks-3.0.2/lib/src/lifecycle_mixin.dart').
    lifecycle_mixin.dart:1
    Try correcting the name to the name of an existing getter, or defining a getter or field named 'AppExitResponse'.
    return AppExitResponse.exit;
    ^^^^^^^^^^^^^^^

Failed to compile application.
Exited

Missing / undocumented override of WidgetsBindingObserver.didPushRouteInformation

This plugin cannot be used without overriding WidgetsBindingObserver.didPushRouteInformation:

  @override
  Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
    // TODO: implement didPushRouteInformation
    throw UnimplementedError();
  }

This is not documented, and happens since updating to the new flutter version:

Flutter 1.22.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision d408d302e2 (9 days ago) • 2020-09-29 11:49:17 -0700
Engine • revision 5babba6c4d
Tools • Dart 2.10.0

This recommends to return didPushRoute(routeInformation.location).

Is this supposed to be done by the user? Or handled by the plugin code?

有没有页面生命周期的方案

这是 App 的生命周期,我想问看看有没有页面的生命周期(就每次页面可见,如导航页面前进回退,都希望有个可见时的回调)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.