I have the following AlertDialog
.
showDialog(
context: context,
child: new AlertDialog(
title: const Text(\"Lo
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');
},
),
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.
This works Prefectly
RaisedButton(
child: Text(
"Cancel",
style: TextStyle(color: Colors.white),
),
color: Colors.blue,
onPressed: () => Navigator.pop(context),
),
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);
}