防止对话框在Flutter中因外部触摸而关闭

u7up0aaq  于 2023-01-21  发布在  Flutter
关注(0)|答案(7)|浏览(246)

在Flutter中,我在异步任务期间为加载程序编写了一个简单的对话框。当我点击外面的对话框关闭时,我如何停止这种行为?

    • 代码**
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"),
                ),
              ],
            ),
          ),
        ));
f3temu5u

f3temu5u1#

有一个名为barrierDismissible的属性,可以传递给showDialog;这使得对话框在外部单击时无效或不可用

showDialog(
  barrierDismissible: false,
  builder: ...
)
z8dt9xmd

z8dt9xmd2#

如果你想防止对话框在按下后退按钮时关闭,请参考以下代码。你必须将AlertDialog Package 在WillPopScope小部件中,并使用返回Future.value(false)的函数使onWillPop属性值。

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: () {
                },
              ),
            ],
          )
        )
      },
    );
cwtwac6a

cwtwac6a3#

仅添加此行

barrierDismissible: false,

就像

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),
          );
        });
2nc8po8w

2nc8po8w4#

屏障可拆除:错误,

按照我下面的描述使用此对话框。showDialog(barrierDismissible:false,生成器//代码//

kmb7vmvb

kmb7vmvb5#

这将禁用设备导航

showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: () async => false,
            child:AlertDialog(
            title: new Text("Alert Title"),
            content: new SingleChildScrollView(
              child: Container(),),
            actions: <Widget>[
              new FlatButton(
                child: new Text("Close"),
                onPressed: () {
                },
              ),
            ],
          )
        )
      },
    );
ruarlubt

ruarlubt6#

始终使用get等顶级Flutter封装

Get.generalDialog(pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,) {
      return SimpleDialog(
        ...
      );
    }, barrierDismissible: false /* its default value */);
ymzxtsji

ymzxtsji7#

如果你没有使用showDialog,或者你使用的是GestureDetector,有一个简单的方法,我刚刚做了,只要把一个GestureDetector放在另一个里面,然后设置onTap动作,如果这是你在两个GestureDetector的情况下,有一个区别,你要把一个动作,在另一个你可以只是让它空,就像这样。

GestureDetector(
    onTap: () { //The Gesture you dont want to afect the rest
      Navigator.pop(context);
    },
    child: Container(
      color: Colors.transparent,
      child:GestureDetector(
            onTap: () {}, //This way is not going to afect the inside widget
            child: Container() //your widget
            )
          )
        )

相关问题