'CupertinoDialog cannot pop when Navigator.pop(context) is called together with a function using a global key
Why can't CupertinoDialog pop when Navigator.pop(context) is called together with a function using a global key?
final GlobalKey <_PlayyButtonState>_playingButtonState = GlobalKey<_PlayyButtonState>();
...
Future<void> showAlert(){
return showCupertinoDialog<void>(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: const Text('Re-write?'),
content: const Text('Are you sure? You will have to write from scratch.'),
actions: <CupertinoDialogAction>[
CupertinoDialogAction(
child: const Text('Re-write',
style: TextStyle(
color: Colors.red
)
),
isDestructiveAction: true,
onPressed: () {
_playingButtonState.currentState._onToggle();
Navigator.pop(context); <---- does not pop after calling the function above
},
)
],
),
);
}
}
Removing the code
_playingButtonState.currentState._onToggle();
allow the CupertinoDialog to pop again
Solution 1:[1]
Instead of this
onPressed: () {
_playingButtonState.currentState._onToggle();
Navigator.pop(context); <---- does not pop after calling the function above
},
Use this:
onPressed: () {
Future.delayed(const Duration(milliseconds: 1000), () {
// Here you can write your code
Navigator.pop(context);
});
_playingButtonState.currentState._onToggle();
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tasnuva Tavasum oshin |
