There are 3 ways the AlertDialog can be dismissed (in my example):
- Tapping outside its barrier. (‘to cancel’)
- Tapping a button A which will call pop(). (‘to proceed’)
- Tapping a button B which will call pop(). (to ‘cancel’)
I wish to run some code in a then() callback after the AlertDialog is closed. This code will be dependent on HOW it was closed.
Note: I am using the then() callback so I can detect if the AlertDialog was dismissed not by a button.
Is this possible?
showDialog(context: context, builder: (context) => AlertDialog(
title: Text('Grant access'),
actions: [
/// Button 'A'
OutlinedButton(onPressed: (){
Navigator.of(context).pop();
/// Some more code
}, child: Text('Grant access')),
/// Button 'B'
OutlinedButton(onPressed: () {
Navigator.of(context).pop();
}, child: Text('Cancel'))
],
)).then((value) async {
// Code after closure. Can I detect what way the AlertDialog was dismissed?
});
2
Answers
You could pass data like Navigator.of(context).pop(‘something’)
then receive its value in .then
But i recommend you create a CustomDialog class with callback, it should be better.
You can use the
await
keyword with theshowDialog()
function, and then check the value returned by the function to determine the dismissal method.