How to run code after showDialog is dismissed in Flutter?

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;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

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);
});

Mobile apps typically reveal their contents via full-screen elements called "screens" or "pages". In Flutter these elements are called routes and they're managed by a Navigator widget. The navigator manages a stack of Route objects and provides methods for managing the stack, like Navigator.push and Navigator.pop.

showDialog(
            context: context,
            child: new AlertDialog(
                title: const Text("Your Title"),
                content: const Text(
                  ...
                   Your Message
                  ...),
                actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: () => Navigator.pop(context),
                ),
              ],
            ),
        );

You can check Official Document


If you have made another StatefulWidget for your DialogBox and onDissmissed, you want to call some function in the "dialog calling widget". You can this snippet.

await showDialog(
      context: context,
      builder: (_) => MenuBox(),
);
print("now closed");

Tags:

Dart

Flutter