0

In my flutter app, I wanted to check if a user is not using the app while they are in Foreground. WidgetsBindingObserver only checks if the app is in the foreground/background. and if the app has resumed, inactive, detached, or paused. but how can I check if the user is not using the app in the foreground at a specific time?

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
  }

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    if (state == AppLifecycleState.detached ||
        state == AppLifecycleState.inactive) return;

    final isBackground = state == AppLifecycleState.paused;

    final isForeground = state == AppLifecycleState.resumed;

    if (isBackground || isForeground) {
      Get.offAll(Login());
    }
  }
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
Mohammed Bekele
  • 727
  • 1
  • 9
  • 25

1 Answers1

2

You need to run a periodic timer that gets reset whenever the user taps the screen.

So set up something global to do just that. Set it to X seconds and reset to 0 when the user tap is detected. If the timer reaches Y seconds you know that it is not used by the user.

Flutter - Detect Tap on the Screen that is filled with other Widgets

https://blog.logrocket.com/understanding-flutter-timer-class-timer-periodic/

Hopes this helps you in the right direction

Fréderic Cox
  • 321
  • 2
  • 7
  • 22
  • Indeed here are some other useful stackoverflow posts: https://stackoverflow.com/questions/59067562/detect-when-user-interact-with-the-app-flutter https://stackoverflow.com/questions/54999917/detect-when-user-is-not-interacting-the-app-in-flutter – Jeff Matchett Nov 12 '22 at 17:49
  • do i need state management and create a global Timer? – Mohammed Bekele Nov 12 '22 at 18:08