How to dismiss an AlertDialog on a FlatButton click?

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

I have the following AlertDialog.

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


        
相关标签:
16条回答
  • 2021-02-03 17:15

    This answer works if you want to pop the dialog and navigate to another view. This part 'current_user_location' is the string the router need to know which view to navigate to.

    FlatButton(
               child: Text('NO'),
               onPressed: () {
                 Navigator.popAndPushNamed(context, 'current_user_location');
                  },
               ),
    
    0 讨论(0)
  • 2021-02-03 17:17

    The accepted answer states how to dismiss a dialog using the Navigator Class. To dismiss a dialog without using Navigator you can set the onPressed event of the button to the following:

    setState((){
      thisAlertDialog = null; 
    });
    

    In case the code above is not self-explanatory it is basically setting the Parent AlertDialog of the FlatButton to null, thus dismissing it.

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

    This works Prefectly

          RaisedButton(
                    child: Text(
                      "Cancel",
                      style: TextStyle(color: Colors.white),
                    ),
                    color: Colors.blue,
                    onPressed: () => Navigator.pop(context),
                  ),
    
    0 讨论(0)
  • 2021-02-03 17:19

    You could wrap your AlertDialog in a async method to make the things clean.

      _showAlertConfirmDelete() async {
        // the response will store the .pop value (it can be any object you want)
        var response = await showDialog(
            context: context,
            builder: (context) => AlertDialog(
                  title: Text('Warn'),
                  content: Text('Really wants to remove the record?'),
                  actions: <Widget>[
                    FlatButton(
                        onPressed: () => Navigator.of(context)
                            .pop(false), 
                        child: Text('No')),
                    FlatButton(
                        onPressed: () => Navigator.of(context).pop(true),
                        child: Text('Yes'))
                  ],
                ));
        // do you want to do with the response.
        print(response);
      }
    
    0 讨论(0)
提交回复
热议问题