简体   繁体   English

如何在 flutter 中禁用下拉菜单

[英]How to disable DropDown in flutter

How to completely disable dropdown so user can not get to list of dropdown and also change color to grey如何完全禁用下拉列表,以便用户无法进入下拉列表并将颜色更改为灰色

DropdownButtonFormField<String>(
      value: selected,
      items: ["Value1", "Value2", "Value3"]
          .map(
            (label) => DropdownMenuItem(
              child: Text(label),
              value: label,
            ),
          )
          .toList(),
      onChanged: (value) {
        setState(() => selected = value);
      },
    )

I tired to set onChange to null but the whole dropdown just disappeared我厌倦了将 onChange 设置为 null 但整个下拉列表消失了

The only way is to set onChange to null and set disabledHint唯一的方法是将onChange设置为 null 并设置disabledHint

DropdownButtonFormField<String>(
      style: TextStyle(color: Colors.grey),
      value: selected,
      disabledHint: Text(selected),  //<-set this one
      items: ["Value1", "Value2", "Value3"]
          .map(
            (label) => DropdownMenuItem(
              child: Text(label),
              value: label,
            ),
          )
          .toList(),
      onChanged: null,
//          (value) {
//          setState(() => selected = value);
//        },
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM