I want to ask user "Yes" or "No", to continue (or not) some action. I have code that shows alert, but don't know how to close automatically alert dialog after user have clicked one of the two buttons.
bool showAlertDialog(BuildContext context, String message) {
bool returnValue = false;
// set up the buttons
Widget cancelButton = ElevatedButton(
child: Text("Cancel"),
onPressed: () { returnValue = false; },
);
Widget continueButton = ElevatedButton(
child: Text("Continue"),
onPressed: () { returnValue = true; },
); // set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Do you want to continue?"),
content: Text(message),
actions: [
cancelButton,
continueButton,
],
); // show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
return returnValue;
}