How to update the Homepage just after the showDialog()
is dismissed/disposed? Doesn\'t seem like it has an onDispose()
function.
Found another
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");
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;
});
},
),
],
),
),
);
}
}
Use;
.whenComplete(funcName/*not funcName() !*/);
after the showDialog()
funcName() {
//Your code
}
There are two approaches.
Use async-await
bool shouldUpdate = await showDialog<bool>(...);
if (shouldUpdate) {
setState(() {
// we should update the UI
});
}
Use then
showDialog<bool>(...).then((shouldUpdate) {
if (shouldUpdate) {
setState(() {
// we should update the UI
});
}
});
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();
},
)
],
);
},
)