Flutter - Back button change to exit app

So I have a case where after a successful login, when back is pressed it will go back to the login form, I've tried to check the token on the initial load but it's not working because it's not loading a new page. The solution for this is using WillPopScope I don't know why it's strikedthrough but it is still working from my end.

If you have a login page and a dashboard, you want to wrap the dashboard code with WillPopScope like the example below :

WillPopScope(
      onWillPop: () async {
        final shouldPop = await showDialog<bool>(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: const Text('Do you want to exit the app?'),
              actionsAlignment: MainAxisAlignment.spaceBetween,
              actions: [
                TextButton(
                  onPressed: () {
                    exit(0);
                  },
                  child: const Text('Yes'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pop(context, false);
                  },
                  child: const Text('No'),
                ),
              ],
            );
          },
        );

        return shouldPop!;
      },
      child: ... your remaining code
   );

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe