1

I need to redirect user to auth page when app is inactive for 5 minutes. I suppose using WidgetsBindingObserver. I detect when app is inactive for 5 minutes, but i don't know how to redirect user to auth page.

Here's part of my code:

@override
  void initState() {
    super.initState();
    homeScreen = widget.homeScreen;
    WidgetsBinding.instance.addObserver(this);
  }

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      if (state == AppLifecycleState.paused) {
        Future.delayed(Duration(seconds: 3), () {
          setState(() {
            // navigate to auth page
          });
        });
      }
    });
  }
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Temirlan
  • 162
  • 1
  • 7

2 Answers2

4

You can use the Navigator:

Navigator.push(context,
      MaterialPageRoute(builder: (context) => AuthPage()));
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I tried it, but it is not working. I got error: "Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.". – Temirlan Sep 26 '19 at 09:51
  • check this: https://stackoverflow.com/questions/44004451/navigator-operation-requested-with-a-context-that-does-not-include-a-navigator/51292613#51292613 – Peter Haddad Sep 26 '19 at 09:51
  • it works, thank you. but now i need to check app state in every page of app :( – Temirlan Sep 26 '19 at 10:10
0

You don't need redirect an app in 5 minutes, you can redirect it when user want to navigate into page (or to do some action) which needed to be authenticated, just log last action timestamp into SharedPreferences and check this timestamp on every needed-auth action.

rstrelba
  • 1,838
  • 15
  • 16