搜索插件、代码仓与 APP 分类

插件详情v0.8.0

flutter_fgbg

Flutter 插件,用于检测应用(非 Flutter 容器)进入后台或前台的状态

AndroidiOS
近 30 天下载
13万
喜欢数
182
Pub 分数
140

安装插件

flutter pub add flutter_fgbg

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_fgbg 是一个 Flutter 插件,可在 Android 和 iOS 上检测应用程序(不仅仅是 Flutter 容器)在前台和后台状态之间切换的情况。它提供了可靠的回调函数,用于处理应用生命周期事件,例如进入后台或返回前台,使开发者能够适当地管理资源、暂停计时器或保存状态。该插件与原生平台 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
});

所属分类

源码仓库

使用此插件的项目