How to dismiss an AlertDialog on a FlatButton click?

前端 未结 16 1313
轮回少年
轮回少年 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: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: [
                    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);
      }
    

提交回复
热议问题