我有这个小部件:
DropdownButtonFormField<String>(
hint: Text(translate('payments.select_frequency')),
value: frequency,
items: frequencies.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
translate("expense.$value"),
style: TextStyle(
color: disabledFrequencies.contains(value) ? Colors.grey : null,
),
),
);
}).toList(),
onChanged: (value) async {
if (!disabledFrequencies.contains(value)) {
setState(() {
frequency = value;
});
}
},
validator: (value) {
if (value == null) {
return translate('fill_field');
}
return null;
},
);
这会生成如下内容:
在这里,我应该能够单击第一个选项,但我可以选择其中任何一个。我不久前在 Flutter 存储库中打开了这个问题,他们提到这不是问题。
那有什么选择?
最佳答案
有enable
DropdownMenuItem
上的属性控制点击辅助功能。
return DropdownMenuItem<String>(
value: value,
enabled: !disabledFrequencies.contains(value), //this
onTap: () {
Whether or not a user can select this menu item. Defaults to
true
.
关于Flutter:禁用 DropdownButtonFormField 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73390386/