【问题标题】:flutter - showDialog's barrierDismissible ignored in ios?颤振-在ios中忽略showDialog的barrierDismissible?
【发布时间】:2020-02-25 04:39:45
【问题描述】:

在 android 中,当您点击使用 showDialog() 创建的(材质)模态对话框的“屏障”/外部/背景时,对话框默认关闭。

当您在 iphone(物理设备)上点击该障碍物时,将忽略要关闭的点击,并且模式保持打开状态。

我意识到有一个“showCupertinoDialog”选项,但我试图避免使用它(它也不会点击障碍关闭)。

flutter/dart 文档没有表明 android -vs- ios 的预期行为有任何差异。我已经包含了一些说明该问题的示例代码,我希望 _showMaterialDialog() 对话框在 iphone 上关闭,但事实并非如此。

知道为什么这不起作用吗?这是一个错误还是预期的行为?谢谢!

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';


class dialogtest extends StatefulWidget {
  @override
  dialogtestState createState() => dialogtestState();
}

class dialogtestState extends State<dialogtest> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
            appBar: AppBar(
              title: Text('Dialog Demo'),
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  RaisedButton(
                    onPressed: () {
                      _showMaterialDialog();
                    },
                    child: Text('Show Material Dialog'),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  RaisedButton(
                    onPressed: () {
                      _showCupertinoDialog();
                    },
                    child: Text('Show Cupertino Dialog'),
                  ),
                ],
              ),
            )));
  }

  void _showMaterialDialog() {
    showDialog(
        context: context,
        barrierDismissible: true, // ********* ignored in ios, defaults to true anyway!!
        builder: (context) {
          return AlertDialog(
            title: Text('Material Dialog'),
            content: Text('This is the content of the material dialog'),
            actions: <Widget>[
              FlatButton(
                  onPressed: () {
                    _dismissDialog();
                  },
                  child: Text('Close')),
              FlatButton(
                onPressed: () {
                  print('HelloWorld!');
                  _dismissDialog();
                },
                child: Text('HelloWorld!'),
              )
            ],
          );
        });
  }

  _dismissDialog() {
    Navigator.pop(context);
  }

  void _showCupertinoDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text('Cupertino Dialog'),
            content: Text('This is the content of the cupertino dialog'),
            actions: <Widget>[
              FlatButton(
                  onPressed: () {
                    _dismissDialog();
                  },
                  child: Text('Close')),
              FlatButton(
                onPressed: () {
                  print('HelloWorld!');
                  _dismissDialog();
                },
                child: Text('HelloWorld!'),
              )
            ],
          );
        });
  }

}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我想出了以防万一有人随机遇到这种行为。

    我在创建项目时使用的是 Flutter 1.7(并且仅使用 android)。为了支持在文本字段外部点击时“失焦”,我使用此处的说明将整个项目包装在 GestureDetector 中:

    https://flutter360.dev/dismiss-keyboard-form-lose-focus

    一切都很完美。当我将应用程序移到 Mac 上进行 iOS 测试时,我安装了 v1.9.1+hotfix.6 附带的新 Flutter。我猜 Flutter 中的某些东西从 1.7 -> 1.9.1 改变了,这打破了这个不受支持的实现。不考虑我的 windows/android 颤振版本(它工作的地方)与我的 mac/ios 版本(它不工作的地方)之间的区别,我认为这是一个 iOS 的东西。将 PC 上的 Flutter 升级到 1.9.1 后,我的 Android 手机上的对话框将不再关闭。

    我仍然需要/想要一种从文本字段中点击以关闭键盘的方法。我正在尝试从这里开始的监听器解决方案,到目前为止它似乎按预期工作:

    Flutter. A way for TextField to detect tap outside

    我想这是对框架未提供开箱即用功能的创造性解决方案的副作用以及了解更新框架的影响的练习。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2020-12-17
      • 2020-01-20
      • 2021-03-06
      • 2021-10-14
      • 1970-01-01
      • 2021-02-14
      相关资源
      最近更新 更多