패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.8.0

flutter_fgbg

앱(플러터 컨테이너가 아님)이 백그라운드 또는 포어그라운드로 전환되었을 때를 감지하는 플러터 플러그인

AndroidiOS
30일 다운로드
13만
좋아요
182
Pub 점수
140

패키지 설치

flutter pub add flutter_fgbg

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

flutter_fgbg는 Android 및 iOS에서 앱(단순히 Flutter 컨테이너가 아니라)이 전경과 배경 상태 간에 전환될 때 이를 감지하는 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
});

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트