I call Navigator pushReplacement to show a new view within my flutter app and want to immediately pop up a simple dialog to introduce the page to the user. (I want the user to be able to see the new view in the background)
If I call showDialog within the build method of a widget, and then subsequently return a widget (scaffold) from the build method I get errors stating that flutter is already drawing a widget. I expect I need to listen on a build complete event and then call showDialog.
Guidance on how to do that much appreciated.
Flutter custom Alert Dialog If you want to implement more advanced custom Dialog, you can use Dialog widget for that. Instead of the AlertDialog , in here we return Dialog widget. The showDialog method will remain the same. You can use a Container widget to set relevant height for the Dialog.
Flutter Full Screen Dialog By Using showDialog method we cannot show full-screen Dialog. If we want to show dialog in full screen we must use showGeneralDialog method provided by Flutter SDK. There are some interesting parameter available in showGeneralDialog method. barrierColor – Change dropshadow outside the dialog.
You can call the dialog from inside 'initState()' dalaying its appearance after the first frame has been drawn.
@override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) async { await showDialog<String>( context: context, builder: (BuildContext context) => new AlertDialog( title: new Text("title"), content: new Text("Message"), actions: <Widget>[ new FlatButton( child: new Text("OK"), onPressed: () { Navigator.of(context).pop(); }, ), ], ), ); }); }
The context
variable is always available inside the State
class. It points to the RenderObject
of this widget. The problem is that in initState()
the context is not yet created so you have to defer its usage after the first frame has been laid out. Then it is available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With