'how to use dropdownbutton with same value?
There are few same values in the dropdown button , when i tap on that it show the error ,is there any way to use the use the dropdown with same values .I have tried using the value :
DropdownButtonHideUnderline(
child: DropdownButton2(
iconEnabledColor: primaryColor,
selectedItemHighlightColor: primaryColor,
hint: Text(
'User type',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: _user_type
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
buttonHeight: 40,
// buttonWidth: doubl,
itemHeight: 40,
),
still getting the error
Solution 1:[1]
Value of every DropdownMenuItem
should be unique. In order to make use of list which have repetitive values, you should have a unique identifier for each.
You can create a model:
class Model {
int id;
String value;
Model(this.id, this.value);
}
You can create list with repetitive values:
List<Model> list = [
Model(0, "a"),
Model(1, "a"),
Model(2, "b"),
Model(3, "b"),
Model(4, "c"),
Model(5, "c"),
];
…and you can use the list like this in your DropdownButton
:
DropdownButton(
value: _selectedValue,
items: list
.map((value) => DropdownMenuItem(
value: value.id, child: Text(value.value)))
.toList(),
onChanged: (value) {
_selectedValue = value as int;
setState(() {});
},
)
Solution 2:[2]
Why don't you try adding a .toSet() before the .map(). The .toSet() should filter your list to unique values.
DropdownButtonHideUnderline(
child: DropdownButton2(
iconEnabledColor: primaryColor,
selectedItemHighlightColor: primaryColor,
hint: Text(
'User type',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: _user_type
.toSet()
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
buttonHeight: 40,
// buttonWidth: doubl,
itemHeight: 40,
),
Solution 3:[3]
Try below code, I have used dropdown_button2
package
Declare your variables and dropdown data
String selectedValue;
List<String> items = [
'User 1',
'User 2',
'User 3',
'User 4',
'User 5',
'User 6',
];
Your Widget:
DropdownButtonHideUnderline(
child: DropdownButton2(
isExpanded: true,
hint: Text(
'User type',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: items
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
color: Colors.black,
),
overflow: TextOverflow.ellipsis,
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
buttonHeight: 50,
buttonWidth: 160,
buttonPadding: EdgeInsets.all(8),
buttonDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: Colors.black26,
),
),
itemHeight: 40,
),
),
You can refer my answer here, here, here also for same using dropdown_below package
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 | Jeremy Caney |
Solution 2 | Dazly Gonsalves |
Solution 3 | Ravindra S. Patil |