FLUTTER ECOSYSTEM

spideythewebhead/flutterlifecyclehooks

spideythewebhead/flutterlifecyclehooks open-source repository details.

flutterlifecyclehooks 项目封面
Stars
15
Forks
3
最近推送(UTC)
2024年6月28日
项目状态
未归档
Pantelis Tsakoulis GitHub avatar
GITHUB User

Pantelis Tsakoulis ↗

DEV.HUTAthens, Greece
语言DartHTMLSwiftKotlinObjective-C

此仓库发布的插件

使用的依赖

依赖清单 4 项
  • flutter{"sdk":"flutter"}
  • flutter_test开发依赖{"sdk":"flutter"}
  • mockito开发依赖^5.4.2
  • flutter_lints开发依赖^2.0.1

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

展开 / 收起项目 README

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.