How to dismiss an AlertDialog on a FlatButton click?

前端 未结 16 1294
轮回少年
轮回少年 2021-02-03 16:44

I have the following AlertDialog.

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text(\"Lo         


        
相关标签:
16条回答
  • 2021-02-03 16:56
    Navigator.of(context, rootNavigator: true).pop('dialog')
    

    worked with me.

    0 讨论(0)
  • 2021-02-03 16:57

    If you don't want to return any result, use either of them:

    Navigator.of(context).pop();
    Navigator.pop(context);
    

    But if you do want to return some result, see this

    Example:

    showDialog(
        context: context,
        builder: (_) {
          return AlertDialog(
            title: Text('Wanna Exit?'),
            actions: [
              FlatButton(
                onPressed: () => Navigator.pop(context, false), // passing false
                child: Text('No'),
              ),
              FlatButton(
                onPressed: () => Navigator.pop(context, true), // passing true
                child: Text('Yes'),
              ),
            ],
          );
        }).then((exit) {
      if (exit == null) return;
    
      if (exit) {
        // user pressed Yes button
      } else {
        // user pressed No button
      }
    });
    
    0 讨论(0)
  • 2021-02-03 17:02
    Navigator.pop(_)
    

    worked for me, but the Flutter Team's gallery contains an example using:

    Navigator.of(context, rootNavigator: true).pop()
    

    which also works, and I am tempted to follow their lead.

    0 讨论(0)
  • 2021-02-03 17:04

    Navigator.pop() should do the trick. You can also use that to return the result of the dialog (if it presented the user with choices)

    0 讨论(0)
  • 2021-02-03 17:04

    pass it in the showDialog barrierDismissible : true

    0 讨论(0)
  • 2021-02-03 17:07

    Generally Navigator.pop(context); works.

    But If the application has multiple Navigator objects and dialogBox doesn't close, then try this

    Navigator.of(context, rootNavigator: true).pop();
    

    If you want to pass result call, try

    Navigator.pop(context,result);
    

    OR

    Navigator.of(context, rootNavigator: true).pop(result)
    
    0 讨论(0)
提交回复
热议问题