flutterflutter-navigationflutter-go-router

GoRouter library can't allow me to send alert exit before exiting the app


    return Scaffold(
  key: HomePage.scaffoldKey,
  drawer: DrawerWithDropdown(),
  backgroundColor: tdWhite,
  body: WillPopScope(
    onWillPop: () async {
      final shouldExit = await showDialog<bool>(
        context: context,
        builder: (context) => AlertDialog(
          title: Text('Confirmation'),
          content: Text('Do you want to exit?'),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: Text('No'),
            ),
            TextButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: Text('Yes'),
            ),
          ],
        ),
      );
      return shouldExit == true;
    },
    child: SafeArea

the problem that i can't make a exit dialog when user want to exit the app the app exit when user press on bottom . i want to make dialog that ask the user want to exit the app. i try to make the willPopscope in the goRouter class and in main and still the app exit without showing dialiog, and this is image of class of goRoute [enter image description here][1]

[1]: https://i.sstatic.net/7YfY1veK.png i want to make the dialog in homepage


Solution

  • You can try the logic inside the goRouter instead on WillPopScope as such,

    GoRoute(
                path: '/home',
                name: 'home',
                onExit: (context, state) async {
                  final shouldExit = await showDialog<bool>(
                    context: context,
                    builder: (context) => AlertDialog(
                      title: const Text('Confirmation'),
                      content: const Text('Do you want to exit?'),
                      actions: [
                        TextButton(
                          onPressed: () => Navigator.of(context).pop(false),
                          child: const Text('No'),
                        ),
                        TextButton(
                          onPressed: () => Navigator.of(context).pop(true),
                          child: const Text('Yes'),
                        ),
                      ],
                    ),
                  );
                  return shouldExit == true;
                },
                pageBuilder: (context, state) => const NoTransitionPage(
                  child: HomeScreen(),
                ),
              ),