简体   繁体   中英

Prevent keyboard dismiss when tapping DropdownButton

This question pertains to Flutter. I have a DropdownButton above a TextField as follows:

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. When you tap the DropdownButton, it removes focus from TextField and thus dismisses the keyboard. 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?

Finally found the solution. Need to wrap the AlertDialog in a SingleChildScrollView. This opens the AlertDialog at the top of the screen and doesn't move it when a keyboard opens/closes. 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,
            .......
          ),
        ],
      ),
    )
  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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