How run code after showDialog is dismissed in Flutter?

前端 未结 7 1437
心在旅途
心在旅途 2021-02-05 01:11

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

Found another

相关标签:
7条回答
  • 2021-02-05 01:22

    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

    0 讨论(0)
  • 2021-02-05 01:22

    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");
    
    0 讨论(0)
  • 2021-02-05 01:30

    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.

    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;
                    });
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2021-02-05 01:32

    Use; .whenComplete(funcName/*not funcName() !*/); after the showDialog()

    funcName() {
    //Your code
    }
    
    0 讨论(0)
  • 2021-02-05 01:32

    There are two approaches.

    1. Use async-await

      bool shouldUpdate = await showDialog<bool>(...);
      if (shouldUpdate) {
        setState(() {
          // we should update the UI
        });
      }
      
    2. Use then

      showDialog<bool>(...).then((shouldUpdate) {
        if (shouldUpdate) {
          setState(() {
            // we should update the UI
          });
        }
      });
      
    0 讨论(0)
  • 2021-02-05 01:36

    Noticed a couple of the above answers were a bit out of date due to the "child" construct used with the AlertDialog object being deprecated now.

    Here is what I use now instead for a blocking alert dialog:

              showDialog(
                context: context,
                builder: (BuildContext context) {
                  // return object of type Dialog
                  return AlertDialog(
                    title: new Text("Conversation Request"),
                    content:
                        new Text("Have a conversation with this person"),
                    actions: <Widget>[
                      // usually buttons at the bottom of the dialog
                      new FlatButton(
                        child: new Text("Accept"),
                        onPressed: () {
                          performAccept();
                        },
                      ),
                      new FlatButton(
                        child: new Text("Ignore"),
                        onPressed: () {
                          performIgnore();
                        },
                      )
                    ],
                  );
                },
              )
    
    0 讨论(0)
提交回复
热议问题