Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code after showDialog is dismissed in Flutter?

Tags:

flutter

dart

How to update the Homepage just after the showDialog() is dismissed/disposed? Doesn't seem like it has an onDispose() function.

Found another possible Answer : The WillPopScope can help detect if the back button is pressed.

The widget that will be used in the showDialog, in its build function the widget can be wrapped in return new WillPopScope(child: ______, onWillPop: _______); The code can be run in the onWillPop function. This can update the Homepage below.

like image 890
Ant D Avatar asked Apr 07 '18 09:04

Ant D


People also ask

How do I use showDialog in Flutter?

In its on the pressed property, we have to use the showDialog widget of flutter. It takes context and a builder. In builder, we provide the AlertDialog widget with title, content(Description of a title), and actions (Yes or no buttons), and our alert dialog box is ready to use.

How do I turn off showDialog Flutter?

pop(result) to close the dialog rather just Navigator. pop(context, result).

How do I know if AlertDialog is showing in Flutter?

You can do it using showGeneralDialog and put dismissible property to false , using this will ensure you that you are the only one who is handling pop up, like in your any action buttons in dialog.


2 Answers

Simply use await or then. the code in then block will be run after the dialog is dismissed.

showDialog(   // Your Dialog Code ).then((val){   Navigator.pop(_context); }); 
like image 117
Jaldip Katre Avatar answered Oct 09 '22 00:10

Jaldip Katre


It really depends on the type of updates you want to have.

However, this is a simple example that may help you figure it out.

enter image description here

class Home extends StatefulWidget {   @override   _HomeState createState() => new _HomeState(); }  class _HomeState extends State<Home> {   String _homeData = "initial data";    @override   Widget build(BuildContext context) {     return new Scaffold(       body: new Center(         child: new Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             new Text(_homeData),             new RaisedButton(               child: new Text("Show Dialog"),               onPressed: ()async{                 bool shouldUpdate = await showDialog(                   context: this.context,                   child:new AlertDialog(                     content: new FlatButton(                       child: new Text("update home"),                       onPressed: () => Navigator.pop(context, true),                     ),                   ),                 );                 setState(() {                   shouldUpdate ? this._homeData = "updated" : null;                 });               },             ),           ],         ),       ),     );   } } 
like image 37
Shady Aziza Avatar answered Oct 09 '22 01:10

Shady Aziza