简体   繁体   English

点击 DropdownButton 时防止键盘关闭

[英]Prevent keyboard dismiss when tapping DropdownButton

This question pertains to Flutter.这个问题与颤振有关。 I have a DropdownButton above a TextField as follows:我在 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;
          },
        ),

The TextField autofocus brings up the keyboard immediately. TextField 自动对焦立即调出键盘。 When you tap the DropdownButton, it removes focus from TextField and thus dismisses the keyboard.当您点击 DropdownButton 时,它会从 TextField 中移除焦点,从而关闭键盘。 This moves things around on the screen and creates a poor UX.这会在屏幕上移动内容并创建糟糕的用户体验。

在此处输入图片说明 在此处输入图片说明

Any suggestions for how to fix this?有关如何解决此问题的任何建议? Is there a way to keep the keyboard up even after the DropdownButton is tapped?即使在点击 DropdownButton 后,有没有办法让键盘保持打开状态?

Finally found the solution.终于找到了解决办法。 Need to wrap the AlertDialog in a SingleChildScrollView.需要将 AlertDialog 包装在 SingleChildScrollView 中。 This opens the AlertDialog at the top of the screen and doesn't move it when a keyboard opens/closes.这会在屏幕顶部打开 AlertDialog,并且在键盘打开/关闭时不会移动它。 It also makes sure that if the content gets too long (vertically), it can slide under the keyboard and user can use scroll gesture to see/interact with hidden content.它还确保如果内容太长(垂直),它可以在键盘下滑动,用户可以使用滚动手势查看隐藏内容/与隐藏内容交互。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM