This article explains you how to customize dropdown widget in flutter applications to display options. In this flutter example, we are using different properties of flutter dropdown widget.
We are using dropdown button widget to display dropdown list in flutter applications. Dropdown widget displays only selected option by default. When we click on this widget, all options are displayed in list. There are multiple properties in this widget to customize it.
Basic Flutter Dropdown
Most important property in dropdown widget is items. We must pass array of options to this attribute. value property is used to set selected item in dropdown widget when user select it. We need to give unique value to each item under DropdownMenuItem widget. So we can get selected item value from onChanged() callback when user selects option from list.
Customize Dropdown in Flutter
We can customize view of dropdown widget using style property like other widgets. We can also change color of dropdown widget using dropdownColor property. We can also elevate dropdown button using elevation property. This value is from 1 to 9.
We can also set custom icon in dropdown button instead of default icon using icon property. We can also change size of dropdown icon using iconSize property. iconDisabledColor property is used to set color of icon when dropdown button is disabled. If we want to set different color for enabled dropdown button then we can use isonEnabledColor.
Dropdown button widget display empty value by default. To avoid this we can set custom text in this widget using hint property. If we do not set hint value and dropdown widget has options then first item is selected.
We can also set hint value when dropdown button widget is disabled. We can achieve this using disabledHint Property. Dropdown widget is disabled if we do not pass any option to items property.
selectedItemBuilder is used to display different custom text for each option in dropdown button when user selects it. For this we need to pass array to selectedItemBuilder property. We must keep order of custom text same as options text.
DropdownButtonHideUnderline widget is used to remove underline of dropdown button. For this we need to wrap DropdownButton widget inside this widget. Expanded property is used to expand dropdown button to full width.