【问题标题】:Is there a way to always display the hint in a DropDownButton?有没有办法总是在 DropDownButton 中显示提示?
【发布时间】:2020-07-14 18:31:09
【问题描述】:

例如,在 TextField 中可以设置一个带有标签文本的 InputDecoration,当没有用户输入时显示在 TextField 的中心,然后在用户输入的文本上方显示。

使用 DropDownButton,我似乎只能在用户进行选择之前显示提示文本,然后它会消失并仅显示用户的选择。有没有办法模仿 TextField 的行为?

谢谢!

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以使用DropDownButtonFormField 小部件而不是DropDownButton 来实现这一点。 DropDownButtonFormField 具有 decoration 属性,可让您使用 labelText 在从列表中选择项目后越过字段顶部​​。下面的示例工作代码:

    return DropdownButtonFormField<String>(
          decoration: InputDecoration(
            labelText: 'select option'
          ),
    
          value: selected,
          items: ["A", "B", "C"]
              .map((label) => DropdownMenuItem(
                    child: Text(label),
                    value: label,
                  ))
              .toList(),
          onChanged: (value) {
            setState(() => selected = value);
          },
        );
    

    输出:

    希望这能回答你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 2022-01-17
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      相关资源
      最近更新 更多