'control & disable a dropdown button in flutter?

I wanted to control a drop-down button and make it unclickable using a button.

Is there any way to make it disable. Basically not allowing it able to change.

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

So this is the code I currently use on the drop-down button, but i cant disabled it.



Solution 1:[1]

This isn't what you want to hear, but I don't think there's currently an easy way. I experimented with simply removing all the items and that causes a nice little crash. Maybe worth raising an issue with the flutter people on github...

There is an alternative that may be good enough for you for now. If you wrap your DropdownButton in an IgnorePointer, when you want it to be disabled you can change IgnorePointer's ignoring property to true.

That way if the user taps on it, it won't do anything.

But you'll probably want to indicate to the user somehow that it's disabled as well, something like setting the hint text (as it's grey).

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

Solution 2:[2]

You can make DropdownButtonFormField or DropdownButton disabled if set onChanged to null, and if you want that dropdown still shows selected value you must set disabledHint. For example:

     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(),
      )

Solution 3:[3]

Just wrap it with IgnorePointer widget to make DropdownButton disable

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

Solution 4:[4]

okay, i found a trick that satisfied me i wanted it hide/show the DropdownButton depending on CheckboxListTile

in StatefulWidget Class first create a function ex:

  _buildDropDown(bool enable) {
    if (enable) {
      return DropdownButton<String>(
        hint: Text("Hint"),
        items: <String>[
          'item 1',
          'item 2',
          'item 3',
        ].map((String value) {
          return new DropdownMenuItem<String>(
            value: value,
            child: new Text(value),
          );
        }).toList(),
        onChanged: (value) {},
      );
    } else { // Just Divider with zero Height xD
      return Divider(color: Colors.white, height: 0.0);
    }
  }

and now in build

bool enable = true;

@override
Widget build(BuildContext context) {
  return Column(
    children: <Widget>[
      CheckboxListTile(
        title: const Text('Switcher'),
        selected: true,
        value: enable,
        onChanged: (bool value) {
          setState(() {
            enable = value;
          });
        },
      ),
      _buildDropDown(enable),
    ],
  );
}

now every time you change enable it will display and hide the DropdownButton

Solution 5:[5]

If items or onChanged is null, the button will be disabled, the down arrow will be grayed out, and the disabledHint will be shown (if provided)

So something like this should work:

DropdownButton<String>(
  ...
  onChanged: this.enabled ? (id) => setState(() => this.id = id) : null,
)

Solution 6:[6]

DropdownButtonFormField(
     onChange: isDisable ? null : (str){
        
     },
     disabledHint: isDisable ?  null : Text('Your hint text'),
     ...
)

For disable onChange: null

For disable Caption disabledHint: Text('Your hint text')

Solution 7:[7]

Simple:

decoration:InputDecoration(enabled: false),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rmtmckenzie
Solution 2 Alex Lysun
Solution 3 iDecode
Solution 4 Saifallak
Solution 5 Sandy Currie
Solution 6 Shamim Shaikh
Solution 7 Emerson Barcellos