skip to Main Content

I want to exit the alert.

WillPopScope(
        onWillPop: () async {
          final shouldExit = await showDialog(
            context: context,
            builder: (context) => CupertinoAlertDialog(
              title: const Text('Exit App'),
              content: const Text('Are you sure you want to exit the app?'),
              actions: [
                CupertinoDialogAction(
                  onPressed: () => Navigator.pop(context, false),
                  child: const Text('Dismiss'),
                ),
                TextButton(
                  onPressed: () => Navigator.pop(context, true),
                  child: Text(
                    'Exit',
                    style:
                    TextStyle(color: Theme.of(context).colorScheme.error),
                  ),
                ),
              ],
            ),
          );

          return shouldExit;
        },

2

Answers


  1. Chosen as BEST ANSWER

    PopScope( canPop: false, onPopInvoked: (didPop) async { debugPrint("didPop1: $didPop"); if (didPop) { return; }

          final bool? shouldPop = await showDialog(
            context: context,
            builder: (context) => CupertinoAlertDialog(
              title: const Text('Exit App'),
              content: const Text('Are you sure you want to exit the app?'),
              actions: [
                CupertinoDialogAction(
                  onPressed: () => Navigator.pop(context, false),
                  child: const Text('Dismiss'),
                ),
                TextButton(
                  onPressed: () => Navigator.pop(context, true),
                  child: Text(
                    'Exit',
                    style:
                    TextStyle(color: Theme.of(context).colorScheme.error),
                  ),
                ),
              ],
            ),
          );
    
          if (shouldPop ?? false) {
            SystemNavigator.pop();
          }
          debugPrint("didPop2: $didPop");
        },
    

  2. you can try this solution

     PopScope(
            canPop: false,
            onPopInvoked: (bool value) async {
              final shouldExit = await showDialog<bool>(
                context: context,
                builder: (context) => CupertinoAlertDialog(
                  title: const Text('Exit App'),
                  content: const Text('Are you sure you want to exit the app?'),
                  actions: [
                    CupertinoDialogAction(
                      onPressed: () => Navigator.pop(context, false),
                      child: const Text('Dismiss'),
                    ),
                    TextButton(
                      onPressed: () => Navigator.pop(context, true),
                      child: Text(
                        'Exit',
                        style:
                            TextStyle(color: Theme.of(context).colorScheme.error),
                      ),
                    ),
                  ],
                ),
              );
              if (shouldExit == true) {
                Navigator.of(context).pop();
              }
            },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search