搜尋套件、儲存庫與 APP 分類

套件詳情v5.0.0

flutterlifecyclehooks

一個 Flutter 套件,讓您輕鬆為應用程式生命週期狀態新增鉤子

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
958
喜歡數
52
Pub 分數
150

安裝套件

flutter pub add flutterlifecyclehooks

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

FlutterLifecycleHooks 是一個輕量級套件,讓開發者能輕鬆將自訂邏輯附加至 Flutter 應用程式的重要生命週期事件。它提供適用於應用程式啟動、背景化及終止等狀態的鉤子,支援所有受支援的平台——包含行動裝置、網頁與桌面端。透過整合這些鉤子,開發者可在應用程式生命週期的精確時刻管理資源、儲存狀態或觸發動作。此套件簡化了生命週期管理,無需撰寫重複程式碼或處理複雜的狀態邏輯。

適用情境

  • 管理應用在恢復時的狀態
  • 應用進入背景時清理資源
  • 在應用程式終止前儲存使用者資料
  • 處理應用啟動時的推送通知
  • 優化生命週期轉換期間的效能

主要特色

  • 基於鉤子的簡單 API
  • 跨平台支援
  • 最小化樣板程式碼
  • 支援所有 Flutter 平台
  • 與現有程式碼的輕鬆整合

適合對象

  • 開發者管理應用生命週期事件
  • 需要對啟動/關閉進行精確控制的應用程式
  • 使用函數式程式設計模式的專案
  • 希望實現清晰生命周期管理的團隊
  • 具有背景處理需求的應用程式

使用建議

  • 需要理解 Flutter 生命週期
  • 如果過度使用,可能會增加複雜性
  • 不適合沒有生命週期邏輯的簡單應用
  • 限於生命週期特定任務
  • 不取代完整的狀態管理解決方案

原始 README

以下為關聯原始碼儲存庫的 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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。