防止在点击DropdownButton时关闭键盘

时间:2020-02-06 20:23:27

标签: flutter

此问题与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移开焦点,从而关闭键盘。这样会在屏幕上四处移动,并导致UX效果不佳。

enter image description here enter image description here

有关如何解决此问题的任何建议?即使点击了DropdownButton,有没有办法使键盘保持向上状态?

1 个答案:

答案 0 :(得分:0)

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

import pandas as pd

steps = (145, 154, 157)

df = pd.read_fwf('path.dat', header=None)

for step in steps:
    # select every 'step' rows
    a = df[step - 1::step]

    print(sum(a[1]), sum(a[2]), sum(a[3]))