控制并禁用颤动中的下拉按钮?

Dan*_*ana 7 flutter

我想控制一个下拉按钮,并使用一个按钮使其无法点击.

有没有办法让它禁用.基本上不允许它能够改变.

new DropdownButton(
          value: animalName,
          items: animals.map(
            (String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text('$value'),
              );
            },
          ).toList(),
          onChanged: (value) {
            setState(() {
              animalName = value;
            });
          },
        ),
Run Code Online (Sandbox Code Playgroud)

所以这是我目前在下拉按钮上使用的代码,但我无法禁用它.

wam*_*ous 13

在DropdownButton文档中找到了这个:

如果items或onChanged为null,则该按钮将被禁用,向下箭头将显示为灰色,并且将显示disabledHint(如果提供)

  • 太棒了!如果有一个“enabled”构造函数参数那就更好了。 (11认同)

Ale*_*sun 7

如果设置为 null,您可以使DropdownButtonFormFieldDropdownButton禁用onChanged,并且如果您希望下拉列表仍显示选定的值,则必须设置disabledHint. 例如:

     DropdownButtonFormField<String>(
        disabledHint: Text(_selectedItem),
        value: _selectedItem,
        onChanged: enabled ? (value) => setState(() => _selectedItem = value) : null,
        items: items.map<DropdownMenuItem<String>>((item) {
          return DropdownMenuItem(
            value: item,
            child: Text(item),
          );
        }).toList(),
      )
Run Code Online (Sandbox Code Playgroud)


Ank*_*dik 7

只需用IgnorePointer小部件包装它即可DropdownButton禁用

IgnorePointer(
      ignoring:  enabled,
      child: new DropdownButton(
          value: animalName,
          items: animals.map(
            (String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text('$value'),
              );
            },
          ).toList(),
          onChanged: (value) {
            setState(() {
              animalName = value;
            });
          },
        ),
);
Run Code Online (Sandbox Code Playgroud)


rmt*_*zie 5

这不是您想听到的,但是我不认为目前有一种简单的方法。我尝试简单地删除所有项目,这导致了一个不错的崩溃。也许值得在github上与扑扑的人提出一个问题...

目前有一种替代方法可能对您已经足够了。如果将DropdownButton包装在IgnorePointer中,则当您希望禁用它时,可以将IgnorePointer的ignoring属性更改为true。

这样,如果用户点击它,它将不会执行任何操作。

但是您可能想要以某种方式向用户指示它也被禁用,例如设置提示文本(因为它是灰色的)。

      child: new IgnorePointer(
        ignoring: true,
        child: new DropdownButton(
          hint: new Text("disabled"),
            items: ["asdf", "wehee", "asdf2", "qwer"].map(
              (String value) {
                return new DropdownMenuItem<String>(
                  value: value,
                  child: new Text('$value'),
                );
              },
            ).toList(),
          onChanged: (value) {},
        ),
Run Code Online (Sandbox Code Playgroud)