Flutter:禁用 DropdownButtonFormField 选项

标签 flutter flutter-dropdownbutton

我有这个小部件:

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;
        },
      );

这会生成如下内容:

enter image description here

enter image description here

在这里,我应该能够单击第一个选项,但我可以选择其中任何一个。我不久前在 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/

相关文章:

Flutter 下载文件到手机下载目录

dart - 如何将整数中的ascii值转换为 flutter 中的等效字符?

flutter - 根据另一个 DropdownButton [FLUTTER] 的值动态更改 DropdownButton 的值

android - 单击 DropdownButton 时应用程序崩溃 (Flutter)

Flutter - 如何在小部件测试中选择 DropdownButton 项

Flutter - 有没有办法查看哪一行代码抛出错误?

firebase 与 flutter 的集成在地理位置依赖性方面不成功

Flutter Getx - 更新子列表中的项目不是 react 性的