How run code after showDialog is dismissed in Flutter?

前端 未结 7 1442
心在旅途
心在旅途 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: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 {
      String _homeData = "initial data";
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                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;
                    });
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    

提交回复
热议问题