flutter check if app is in background code example

Example: flutter check if app is in foreground

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  late AppLifecycleState _notification; 
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _notification = state;
    });
  }

  @override
  initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
    ...
  }

  @override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    super.dispose();
  }
}

Tags:

Misc Example