How to dismiss an AlertDialog on a FlatButton click?

前端 未结 16 1373
轮回少年
轮回少年 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:13

    Example of dismissing alert dialog on flat button click

    RaisedButton(
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                        title: Text('Are you sure?'),
                        content: Text('Do you want to remove item?'),
                        actions: [
                          FlatButton(
                              onPressed: () => Navigator.of(context).pop(false),//  We can return any object from here
                               child: Text('NO')),
                          FlatButton(
                              onPressed: () => Navigator.of(context).pop(true), //  We can return any object from here
                              child: Text('YES'))
                        ],
                      )).then((value) =>
                  print('Selected Alert Option: ' + value.toString()));
            },
            child: Text('Show Alert Dialog'),
          ),
    

    Above code have two unique things which is used to provide callback result of dialog

    Navigator.of(context).pop(false) -- return false value when we pressed NO Navigator.of(context).pop(true) -- return true value when we pressed YES

    Based on these return value, we can perform some operation outside of it or maintain the dialog status value

提交回复
热议问题