Prevent dialog from closing on outside touch in Flutter
AndroidIosDialogDartFlutterAndroid Problem Overview
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.
Android Solutions
Solution 1 - Android
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: ...
)
Solution 2 - Android
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: () {
},
),
],
)
)
},
);
Solution 3 - Android
just Add this Line
barrierDismissible: false,
like as
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
"Classes",
style: TextStyle(
fontSize: 24, color: Colors.black, fontFamily: 'intel'),
),
content: setupAlertDialoadClassList(
context, listClasses, Icons.class__outlined, 0),
);
});
Solution 4 - Android
Always use top flutter packages like get
Get.generalDialog(pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,) {
return SimpleDialog(
...
);
}, barrierDismissible: false /* its default value */);
Solution 5 - Android
barrierDismissible: false,
Use this one as I described below. showDialog( barrierDismissible: false, builder // code //