【问题标题】:Prevent keyboard dismiss when tapping DropdownButton点击 DropdownButton 时防止键盘关闭
【发布时间】:2020-05-22 23:16:32
【问题描述】:

这个问题与 Flutter 有关。我在 TextField 上方有一个 DropdownButton,如下所示:

DropdownButton<String>(
          isExpanded: true,
          hint: Text(associatedHint),
          disabledHint: Text(associatedHint),
          items: diagnosesList.map((int value) {
            return DropdownMenuItem<String>(
              value: value.toString(),
              child: Text(dxDisplay),
            );
          }).toList(),
          value: ANID,
          onChanged: (String newANID) {
            setState(() {
              ANID = newANID;
            });
          },
        ),
        TextField(
          autofocus: true,
          keyboardType: keyboardType,
          maxLines: maxLines,
          textCapitalization: TextCapitalization.sentences,
          controller: _textEntryController,
          decoration: InputDecoration(hintText: "Entry"),
          onChanged: (value) {
            noteEntry = value;
          },
        ),

TextField 自动对焦会立即调出键盘。当您点击 DropdownButton 时,它会从 TextField 中移除焦点,从而关闭键盘。这会在屏幕上移动东西并创建糟糕的用户体验。

关于如何解决此问题的任何建议?即使在点击 DropdownButton 后,有没有办法让键盘保持打开状态?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    终于找到了解决办法。需要将 AlertDialog 包装在 SingleChildScrollView 中。这会在屏幕顶部打开 AlertDialog,并且在键盘打开/关闭时不会移动它。它还确保如果内容过长(垂直),它可以在键盘下滑动,用户可以使用滚动手势查看隐藏内容/与隐藏内容交互。

    return SingleChildScrollView(
        child: AlertDialog(
          title: alertTitle,
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget> [
              DropdownButton<String>(
                .......
              ),
              TextField(
                autofocus: true,
                .......
              ),
            ],
          ),
        )
      );
    

    【讨论】:

      猜你喜欢
      • 2018-01-15
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多