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());
    }
  }

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed here are some other useful stackoverflow posts: stackoverflow.com/questions/59067562/… stackoverflow.com/questions/54999917/…
do i need state management and create a global Timer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.