how to use dropdownbutton with same value? - flutter

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

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

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

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,
),
),
Your Dropdown button result->
Your Dropdown data result->
You can refer my answer here, here, here also for same using dropdown_below package

Related

Reset DropDownMenu

I have a page with a DropdownButton.
The hint is 'Select your choice'.
When an item is selected in the DropdownButton when I click on a button I want the DropdownButton to go back to the beginning and show 'Select your choice' again.
child: DropdownButton<String> (
hint: Text(
'Select your choice',
style: TextStyle(
color: Colors.white,
fontFamily: 'Quicksand',
),
),
borderRadius: BorderRadius.circular(10),
dropdownColor: Color.fromRGBO(84, 84, 84, 10),
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.white,
),
iconSize: 40,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontFamily: 'Quicksand',
),
items: [
DropdownMenuItem(
child: Text(listaMedidas[0]),
value: listaMedidas[0],
),
DropdownMenuItem(
child: Text(listaMedidas[1]),
value: listaMedidas[1],
),
DropdownMenuItem(
child: Text(listaMedidas[2]),
value: listaMedidas[2],
),
DropdownMenuItem(
child: Text(listaMedidas[3]),
value: listaMedidas[3],
),
],
onChanged: (value) => setState(() {
escolha = value;
limpar();
textoObservacao(value);
}),
value: escolha,
),
You can set null value to the DropdownButton, it will show the hint text.
class DropDownButtonTest extends StatefulWidget {
const DropDownButtonTest({Key? key}) : super(key: key);
#override
State<DropDownButtonTest> createState() => _DropDownButtonTestState();
}
class _DropDownButtonTestState extends State<DropDownButtonTest> {
List<String> listaMedidas = ["A", "B"];
String? escolha;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
DropdownButton<String>(
hint: Text(
'Select your choice',
style: TextStyle(
color: Colors.white,
fontFamily: 'Quicksand',
),
),
borderRadius: BorderRadius.circular(10),
dropdownColor: Color.fromRGBO(84, 84, 84, 10),
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.white,
),
iconSize: 40,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontFamily: 'Quicksand',
),
items: [
for (int i = 0; i < listaMedidas.length; i++)
DropdownMenuItem(
child: Text(listaMedidas[i]),
value: listaMedidas[i],
),
],
onChanged: (value) => setState(() { escolha = value;}),
value: escolha,
),
ElevatedButton(
onPressed: () {
setState(() {
escolha = null;
});
},
child: Text("Reset"),
),
],
),
);
}
}

Show form based on item selected in DropdownButtonFormField flutter

I am using this dropdownbuttonform to allow user to make selection from a list of items. When user select Item 1, I want to show a form for the user to enter the details. I don't know if DropdownButtonFormField is the right widget to use and how to go about it.
This is a snippet of my drop down form
List items = ['Item 1', 'Item 2' 'Item 3', 'Item 4'];
DropdownButtonFormField(
value: selectedStatus,
onChanged: ( value) {
setState(() {
selectedStatus = value.toString();
});
},
selectedItemBuilder: (context) => status.map((item) {
return DropdownMenuItem(
value: item,
child: Text(
item,
style: TextStyle(
color: Color(0xFF0E0E0E),
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
);
}).toList(),
decoration: InputDecoration(
border: InputBorder.none,
filled: true,
fillColor: Colors.white,
),
items: status.map((status) {
return DropdownMenuItem<String>(
value: status,
child: Text(
status,
style: TextStyle(
color: Colors.black,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
);
}).toList(),
),
You can use conditional if to present the new widget. like
child: Column(
children: [
DropdownButtonFormField(..),
if (selectedStatus == items[0]) TextFormField()
Demo
List items = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];
String? selectedStatus;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
child: Column(
children: [
DropdownButtonFormField(
value: selectedStatus,
onChanged: (value) {
setState(() {
selectedStatus = value.toString();
});
},
items: items.map((status) {
return DropdownMenuItem<String>(
value: status,
child: Text(
status,
style: TextStyle(
color: Colors.black,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
);
}).toList(),
),
//or you can use `Item 1`
if (selectedStatus == items[0]) TextFormField()
],
),
),
);
}
}

flutter remove DropdownButton left padding

I've struggled to remove flutter's DropdownButton left padding so it can match the TextField below. Anyone can help?
Here's my code:
DropdownButton<MenuModel>(
itemHeight: itemHeight,
isExpanded: true,
underline: null,
value: currentValue,
hint: Text(placeholder, style: hintStyle),
style: textStyle,
disabledHint: Text(currentValue?.label ?? placeholder, style: textStyle),
onChanged: widget.modalMode ? null : _onChanged,
items: widget.listMenu.map<DropdownMenuItem<MenuModel>>((MenuModel val) {
return DropdownMenuItem<MenuModel>(
child: Text(val.label),
value: val,
);
}).toList(),
)
finally I've found the problem. So it happen because I wrapped the DropdownButton with ButtonTheme(alignedDropdown: true)
ButtonTheme(
alignedDropdown: true, // it's the culprit
child: DropdownButton<MenuModel>(
itemHeight: itemHeight,
isExpanded: true,
underline: null,
value: currentValue,
hint: Text(placeholder, style: hintStyle),
style: textStyle,
disabledHint: Text(currentValue?.label ?? placeholder, style: textStyle),
onChanged: widget.modalMode ? null : _onChanged,
items: widget.listMenu.map<DropdownMenuItem<MenuModel>>((MenuModel val) {
return DropdownMenuItem<MenuModel>(
child: Text(val.label),
value: val,
);
}).toList(),
),
)
Thanks for the help.
Try below code hope its helpful to you. refer Dropdown here. just change my widget to your widgets
Create variable and List of dropdown:
var selectedCategory;
List categoryDropDownList = [
'One',
'Two',
'Three',
'Four',
];
Your Widget:
DropdownButton(
isDense: true,
isExpanded: true,
hint: Text(
'Select Category',
style: TextStyle(
color: Colors.black,
fontSize: 15,
),
textAlign: TextAlign.center,
),
value: selectedCategory,
items: categoryDropDownList.map<DropdownMenuItem<String>>(
(value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value.toString(),
style: TextStyle(
fontSize: 17,
),
),
);
},
).toList(),
onChanged: (var value) {
setState(
() {
selectedCategory = value;
},
);
},
),
Your result Screen->
I have tried to use the same style as you are using and managed to get that done with the following code. I hope that works for you. I am attaching the screenshot as well of the output.
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
DropdownButton<String>(
itemHeight: 50,
isExpanded: true,
underline: null,
value: dropdownValue,
hint: const Text('Honorific', style: TextStyle(color: Colors.grey)),
icon: const Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['Honorific']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
TextFormField(
controller: controller,
validator: (value) {
if (value!.isEmpty || !value.contains('#')) {
return 'Please enter a valid email address';
}
return null;
},
textInputAction: TextInputAction.next,
keyboardType: TextInputType.emailAddress,
),
],
),
),
output

How to set padding for items in dropdown button when clicked in Flutter application

I'm trying to set margin/padding top for the list of items that come in dropdown button when clicked. How to set padding for dropdown button.
After clicking dropdown button I get the items on top of the view like this,
I need to get the items below the dropdown button.
I tried giving padding inside a container but only the dropdown button seems to be moving and not the items in it.
Is there a solution for this?
Code for reference,
ButtonTheme(
alignedDropdown: true,
padding: EdgeInsets.only(left: 0.0, right: 0.0, top: 100, bottom: 0.0),
child:
DropdownButton(
menuMaxHeight: 300.0,
hint: _dropDownValue == null
? Text("---SELECT---", style: TextStyle(color: Colors.black))
:
Text(
_dropDownValue,
style: TextStyle(color: Colors.blue, fontSize: 20),
),
isExpanded: true,
iconSize: 30.0,
icon: Icon(Icons.arrow_drop_down_circle),
iconDisabledColor: Colors.red,
iconEnabledColor: Colors.green,
style: TextStyle(color: Colors.black),
dropdownColor: Colors.white,
items: answer.map(
(val) {
return DropdownMenuItem<String>(
value: val,
child: Text(val),
);
},
).toList(),
onChanged: (val) {
setState(
() {
_dropDownValue = val;
},
);
},
),
),
Try below code hope its helpful to you . you must used dropdown_below from here
Create your list
List numberList = [
{'no': 1, 'number': '1'},
{'no': 2, 'number': '2'},
{'no': 3, 'number': '3'},
{'no': 4, 'number': '4'},
{'no': 5, 'number': '5'},
{'no': 6, 'number': '6'},
{'no': 7, 'number': '7'},
{'no': 8, 'number': '8'},
{'no': 9, 'number': '9'},
];
One varibale and list our value
List<DropdownMenuItem<Object?>> _dropdownTestItems = [];
var selectedNumber;
Create initState() and dispose() method:
#override
void initState() {
_dropdownTestItems = buildDropdownTestItems(numberList);
super.initState();
}
#override
void dispose() {
super.dispose();
}
Add your selected number value in dropdown
List<DropdownMenuItem<Object?>> buildDropdownTestItems(List numberList) {
List<DropdownMenuItem<Object?>> items = [];
for (var i in numberList) {
items.add(
DropdownMenuItem(
value: i,
child: Text(
i['number'],
style: TextStyle(color: Colors.black),
),
),
);
}
return items;
}
Your Widget:
Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownBelow(
itemWidth: 100,
itemTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.black),
boxTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.white54),
boxPadding: EdgeInsets.fromLTRB(13, 12, 13, 12),
boxWidth: 100,
boxHeight: 45,
boxDecoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(
width: 1,
color: Colors.black,
),
),
icon: Icon(
Icons.arrow_downward,
color: Colors.black,
),
hint: Text(
'Select',
style: TextStyle(
color: Colors.black,
),
),
value: selectedNumber,
items: _dropdownTestItems,
onChanged: (selectedTest) {
setState(() {
selectedNumber = selectedTest;
});
},
),
),
Your result screen->

How to add String to Dropdownbutton

I have map<dynamic, String> with units and names.
Map timeUnits = <dynamic, String>{
TimeUnit.second: 'second',
TimeUnit.minute: 'minute',
TimeUnit.hour: 'hour',
TimeUnit.day: 'day',
TimeUnit.week: 'week',
TimeUnit.month: 'month',
TimeUnit.calendarYear: 'year',
};
How to add String to Dropdownbutton? Dropdownbutton shows this problem "The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter type 'List'."
Dropdownbutton code:
child: DropdownButton(
isExpanded: true,
underline: Container(),
value: currentUnits[widget.convertorType],
icon: Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 6),
child: Image.asset("assets/icons/down-arrow2.png"),
),
iconSize: 35,
elevation: 20,
style: TextStyle(
fontSize: 18,
color: Color.fromRGBO(114,137,218, 1)
),
items: widget.items
.map(
(e) => DropdownMenuItem(
child: e == TimeUnit.calendarYear
? Text("year")
: e.toString().length == 3
? Text(e)
: Text(e.toString().split(".")[1]),
value: e,
),
)
.toList(),
onChanged: (newValue) {
setState(() {
updateCurrentUnit(widget.convertorType, newValue);
widget.calculate();
});
},
),
As per my understanding, it is due to the (items: widget.item) error
so change it to (items: widget.items.entries) and e to e.key or e.value
DropdownButton(
isExpanded: true,
underline: Container(),
value: currentUnits[widget.convertorType],
icon: Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 6),
child: Image.asset("assets/icons/down-arrow2.png"),
),
iconSize: 35,
elevation: 20,
style: TextStyle(
fontSize: 18,
color: Color.fromRGBO(114,137,218, 1)
),
items: widget.items.entries
.map(
(e) => DropdownMenuItem(
child: e.key == TimeUnit.calendarYear
? Text("year")
: e.toString().length == 3
? Text(e.key)
: Text(e.value),
value: e,
),
)
.toList(),
onChanged: (newValue) {
setState(() {
updateCurrentUnit(widget.convertorType, newValue);
widget.calculate();
});
},
)
first, you have to convert your Map to a list then create a list of dropDownMenuItem and then send it over to dropdown widget
for example in the code below provinces is a list and provincesItems is a List of dropdownMenueItem, i fill provincesItems with items I desire from provinces list and then send it over to dropDown Widget
for (int i = 0; i < provinces.length; i++) {
provincesItems.add(DropdownMenuItem(
value: i,
child: Column(
children: [
Text(
provinces[i].persianName,
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.normal),
),
],
),
));
}