Detecting when user returns to app

Best Practices

I'm not sure if my solution is the best, but it works.


// Castom Action: onAppBackground


// Automatic FlutterFlow imports
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/actions/actions.dart' as action_blocks;
import 'package:ff_theme/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

Future onAppBackground() async {
  WidgetsBinding.instance.addObserver(AppLifecycleObserver());
}

class AppLifecycleObserver with WidgetsBindingObserver {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed) {

      FFAppState().update(() {
        FFAppState().AppState = "resumed";
      });

    } else if (state == AppLifecycleState.paused) {

      FFAppState().update(() {
        FFAppState().AppState = "paused";
      });
    }
  }
}
// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the green button on the right!
main.dart :

  await actions.onAppBackground();

and then add the Visibility Detector from Marketplace

https://marketplace.flutterflow.io/item/m9sURUnLbwrxkWwt1Tfp

you will need to add a dependency to your project : visibility_detector: ^0.2.0

Do you have a sample code to avoid using a Visibility Detector library?


PS. Thank you Kinari KURAMOTO


1
3 replies