There are 3 ways the AlertDialog can be dismissed (in my example):
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?
});
You could pass data like Navigator.of(context).pop('something') then receive its value in .then
showDialog(context: context, builder: (context) => AlertDialog(
title: Text('Grant access'),
actions: [
OutlinedButton(onPressed: (){
Navigator.of(context).pop('Button A');
/// Some more code
}, child: Text('Grant access')),
OutlinedButton(onPressed: () {
Navigator.of(context).pop('Button B');
}, child: Text('Cancel'))
],
)).then((value) async {
// Check if the value returned from showDialog is null
if (value == null) {
// If value is null, it means that the dialog was closed by tapping outside
print('Dialog closed by tapping outside');
} else {
// If value is not null, it means that the dialog was closed with a value
// Print the value for debugging purposes
print('Dialog closed with value: $value');
}
});
But i recommend you create a CustomDialog class with callback, it should be better.