flutter_fgbg 0.2.2 copy "flutter_fgbg: ^0.2.2" to clipboard
flutter_fgbg: ^0.2.2 copied to clipboard

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

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.

Checkout example/ project to see the differences in action.

Getting Started #

Add to your pubpsec:

flutter pub add flutter_fgbg

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.stream.listen((event) {
    print(event); // FGBGType.foreground or FGBGType.background
});

// in dispose
subscription.cancel();
137
likes
120
pub points
97%
popularity

Publisher

unverified uploader

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_fgbg