Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open flutter dialog after navigation

Tags:

flutter

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.

like image 400
Tom Avatar asked Jun 11 '18 21:06

Tom


People also ask

How do I show custom dialog in Flutter?

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.

How do I open dialog full screen in Flutter?

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.


1 Answers

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.

like image 100
chemamolins Avatar answered Sep 28 '22 07:09

chemamolins