【问题标题】:Flutter keyboard dismissed when showing alertDialog显示 alertDialog 时 Flutter 键盘消失
【发布时间】:2020-04-18 16:35:01
【问题描述】:

我的 Flutter 应用中有一个文本字段,用户可以在其中输入他们的姓名。他们可以按“提交”。但是当名字太短时,我会显示一个对话框。

问题:当显示对话框时,键盘会自动关闭,并且警告对话框会从上方(当键盘仍处于活动状态时)跳到下方(当键盘不再处于活动状态时,半秒后)。

                        showDialog(
                            context: context,
                            builder: (_) => new AlertDialog(
                                  title: Text("Error"),
                                  content:
                                      Text("Name too short"),
                                ));

是否可以在显示 AlertDialog 时保持键盘处于活动状态?

谢谢!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我不相信有一种方法可以做你想做的事,但我有点想出 hack 试试看,看看你的想法

    Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                TextFormField(
                  onFieldSubmitted: (String value) async {
                    if (value.length < 3) {
                      return await showDialog(
                        context: context,
                        builder: (_) => AlertDialog(
                          title: Text("Error"),
                          content: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              AbsorbPointer(
                                child: TextFormField(
                                  cursorColor: Colors.white,
                                  decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: " Name too short",
                                    hintStyle: TextStyle(color: Colors.black),
                                  ),
                                  autofocus: true,
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    }
                  },
                )
              ],
            ),
          ),
    

    【讨论】:

    • 谢谢!是的,它确实有效,但它有点hacky。如果还有其他不那么骇人听闻的答案,我将等待。否则,我会接受的!谢谢!
    • 没问题,我意识到这是一个 hack,但是没有本地方法可以通过警报对话框保持键盘打开,您可以创建自己的对话框类,但这将是很多工作来阻止键盘关闭。如果您想等待,我知道
    • 你有没有找到更好的答案
    • 请点赞,这样其他人可以更轻松地找到我点赞您的帖子:)
    【解决方案2】:

    尝试这样做:

    showDialog(
          context: context,
          builder: (_) {
             SystemChannels.textInput.invokeMethod('TextInput.show');
             return AlertDialog(
                title: Text("Error"),
                content: Text("Name too short"),
             );
         },
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2020-10-31
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      相关资源
      最近更新 更多