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

套件詳情v0.8.0

flutter_fgbg

Flutter 插件,用於檢測應用(非 Flutter 容器)進入背景或前景的狀態

AndroidiOS
近 30 天下載
13萬
喜歡數
182
Pub 分數
140

安裝套件

flutter pub add flutter_fgbg

套件介紹

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

flutter_fgbg 是一個 Flutter 插件,可檢測應用程式(不僅僅是 Flutter 容器)在 Android 和 iOS 上於前景與背景狀態之間切換的情況。它提供可靠的回呼函數,用於處理應用程式生命週期事件,例如進入背景或返回前景,讓開發者能夠適當地管理資源、暫停計時器或儲存狀態。該插件與原生平台 API 無縫整合,確保在不同裝置條件下都能準確檢測。

適用情境

  • 處理背景任務
  • 暫停媒體播放
  • 儲存使用者狀態
  • 管理位置更新
  • 優化電池使用

主要特色

  • 跨平台支援(Android 與 iOS)
  • 原生層級生命週期檢測
  • 簡單的事件驅動式 API
  • 輕量且高效
  • 即時前後台狀態變更

適合對象

  • 需要精確生命週期感知的應用
  • 媒體或遊戲應用
  • 位置敏感的應用程式
  • 省電應用
  • 背景處理工作流程

使用建議

  • 需要正確處理背景執行限制
  • 可能不會在所有設備製造商的設備上立即觸發
  • 僅限應用層狀態,不包含單個Flutter組件
  • 不支援網頁或桌面平台
  • 依賴於原生作業系統的行為,可能會有所不同

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Flutter Foreground/Background Event Notifier

Flutter plugin to detect when app(not Flutter container) goes to background or foreground.

Why

Flutter has WidgetsBindingObserver to get notified when app changes its state from active to inactive states and back. But it actually includes the state changes of the embedding Activity/ViewController as well. So if you have a plugin that opens a new activity/view controller or in iOS if you start a FaceID prompt then WidgetsBindingObserver will report as the app is inactive/resumed.

This plugin on the other hand reports the events only at app level. Since most apps need only background/foreground events this plugin is implemented with just those events. In iOS, plugin reports didEnterBackgroundNotification and willEnterForegroundNotification notifications and in Android, plugin reports these using androidx.lifecycle:lifecycle-process package. In other platforms plugin fallbacks to AppLifecycleListener API for signals.

Checkout example/ project to see the differences in action.

Getting Started

Add to your pubpsec:

flutter pub add flutter_fgbg

Do a clean build to integrate the plugin native code to app. Otherwise you might encounter MissingPluginException.

Use the built in Widget:

FGBGNotifier(
    onEvent: (event) {
        print(event); // FGBGType.foreground or FGBGType.background
    },
    child: ...,
)

Or consume the event stream directly:

import 'package:flutter_fgbg/flutter_fgbg.dart';

StreamSubscription<FGBGType> subscription;

...
// in initState
subscription = FGBGEvents.instance.stream.listen((event) {
    print(event); // FGBGType.foreground or FGBGType.background
});

// in dispose
subscription.cancel();

Caveats

In newer Android versions and image_picker library, picking an image will open a different app this will make Flutter app switch to background/foreground and flutter_fgbg will report this. At the moment there is no solution to this since the API is working as intented. Tracking the issue here.

As a work around you can use FGBGEvents.ignoreWhile API like this:

FGBGEvents.ignoreWhile(() async {
    await picker.pickImage(source: ImageSource.gallery);
    // or do something else that can put app to background but don't want to be handled by flutter_fgbg
});

所屬分類

原始碼儲存庫

使用此套件的專案