The DropdownButton
widget in Flutter is used to create a dropdown menu that allows the user to select one value from a list of items. Some of the common properties of the DropdownButton
widget are:
items
: This property takes a list ofDropdownMenuItem
objects, which represent the items that will be displayed in the dropdown menu. EachDropdownMenuItem
has avalue
and achild
property, where thevalue
is the data that will be returned when the item is selected, and thechild
is the widget that will be displayed for the item.value
: This property sets the initial value of the dropdown menu. Thevalue
should match thevalue
of one of theDropdownMenuItem
objects in theitems
list.onChanged
: This property takes a callback function that will be called whenever the user selects a new item from the dropdown menu. The callback function takes the newvalue
as its argument.hint
: This property takes a widget that will be displayed as a hint in the dropdown button when no value has been selected.
Here’s an example of how to use the DropdownButton
widget with some of its properties:
DropdownButton<String>(
value: selectedValue,
items: ['Red', 'Green', 'Blue'].map((value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedValue = value;
});
},
hint: Text('Select a color'),
)
In this example, the DropdownButton
widget is used to allow the user to select one of the colors “Red”, “Green”, or “Blue”. The value
property is used to set the initial selected value, and the onChanged
property is used to specify a callback function that will be called when the user selects a new value. The hint
property is used to display a hint in the dropdown button when no value has been selected.