Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent dialog from closing on outside touch in Flutter

In Flutter i write simple dialog for loader during async task. when i touch outside dialog dismissed, How can I stop this behaviour ?

Code

  showDialog(     context: context,     builder: (_) => new Dialog(           child: new Container(             alignment: FractionalOffset.center,             height: 80.0,             padding: const EdgeInsets.all(20.0),             child: new Row(               mainAxisSize: MainAxisSize.min,               children: [                 new CircularProgressIndicator(),                 new Padding(                   padding: new EdgeInsets.only(left: 10.0),                   child: new Text("Loading"),                 ),               ],             ),           ),         )); 

Any help will be appreciated, thank you in advance.

like image 536
Magesh Pandian Avatar asked Jun 01 '18 18:06

Magesh Pandian


People also ask

How do I stop dialog close on click outside Flutter?

To make your AlertDialog widget not close when user taps on the screen space outside the alert box, set barrierDismissible property to false in showDialog() helper method. By default, if you do not provide any value to barrierDismissable property, alert dialog closes when user taps on the area outside the alert.

How do you make dialog non dismissable in Flutter?

There is a property inside showDialog called barrierDismissible . Setting this value to false will make your AlertDialog not closable by clicking outside.

How do I stop showDialog on Flutter?

If you don't want to return any result after showDialog is closed, you can use it. Navigator. pop(context);

How do I use showDialog in Flutter?

In its on the pressed property, we have to use the showDialog widget of flutter. It takes context and a builder. In builder, we provide the AlertDialog widget with title, content(Description of a title), and actions (Yes or no buttons), and our alert dialog box is ready to use.


2 Answers

There's a property called barrierDismissible that you can pass to showDialog ; which makes dialogs dismissible or not on external click

showDialog(   barrierDismissible: false,   builder: ... ) 
like image 134
Rémi Rousselet Avatar answered Oct 02 '22 02:10

Rémi Rousselet


If you want to prevent dialog close when back button pressed then refer below code. You have to wrap your AlertDialog in WillPopScope widget and make onWillPop property value with function which return Future.value(false).

showDialog(       barrierDismissible: false,       context: context,       builder: (BuildContext context) {         return WillPopScope(             onWillPop: () => Future.value(false),             child:AlertDialog(             title: new Text("Alert Title"),             content: new SingleChildScrollView(               child: Container(),),             actions: <Widget>[               new FlatButton(                 child: new Text("Close"),                 onPressed: () {                 },               ),             ],           )         )       },     ); 
like image 36
Sachin Tanpure Avatar answered Oct 02 '22 02:10

Sachin Tanpure