パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.8.0

flutter_fgbg

アプリ(Flutterコンテナではない)がバックグラウンドまたはフォアグラウンドに移行したことを検出するFlutterプラグイン

AndroidiOS
30日間ダウンロード
13万
いいね数
182
Pub ポイント
140

パッケージをインストール

flutter pub add flutter_fgbg

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

flutter_fgbg は、Android および iOS でアプリ(Flutter コンテナだけでなく)がフォアグラウンドとバックグラウンドの状態を切り替えるタイミングを検出するための Flutter プラグインです。アプリのライフサイクルイベント(バックグラウンドに移行する、またはフォアグラウンドに戻るなど)に対して信頼性の高いコールバックを提供し、開発者がリソースを管理したり、タイマーを一時停止したり、状態を適切に保存したりできるようにします。プラグインはネイティブプラットフォームAPIとシームレスに統合され、さまざまなデバイス環境でも正確な検出を実現します。

用途

  • バックグラウンドタスクの処理
  • メディア再生の一時停止
  • ユーザー状態の保存
  • 場所の更新を管理する
  • バッテリー消費の最適化

主な機能

  • クロスプラットフォーム対応(Android および iOS)
  • ネイティブレベルのライフサイクル検出
  • シンプルなイベントベースの API
  • 軽量で効率的
  • リアルタイムのフォアグラウンド/バックグラウンド状態の変更

適している対象

  • 正確なライフサイクル認識を必要とするアプリ
  • メディアまたはゲームアプリ
  • 場所に応じたアプリケーション
  • バッテリーに配慮したアプリ
  • バックグラウンド処理ワークフロー

注意事項

  • バックグラウンド実行制限の適切な処理が必要です
  • すべてのデバイスメーカーのデバイスで即座にトリガーされない可能性があります
  • アプリレベルの状態に限定され、個々のFlutterウィジェットではない
  • Web やデスクトッププラットフォームはサポートされていません
  • ネイティブOSの動作に依存しており、環境によって異なる可能性があります

元の 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
});

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト