gpt4 book ai didi

flutter - 有没有办法始终在 DropDownButton 中显示提示?

转载 作者:行者123 更新时间:2023-12-04 12:16:44 24 4
gpt4 key购买 nike

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

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

谢谢!

最佳答案

您可以使用 DropDownButtonFormField 来实现这一点。小部件而不是 DropDownButton . DropDownButtonFormFielddecoration允许您使用的属性 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);
},
);

输出:

enter image description here

enter image description here

希望这能回答你的问题。

关于flutter - 有没有办法始终在 DropDownButton 中显示提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60997463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com