What is a Dropdown in Flutter?
A dropdown is a UI component that allows the user to select one option from a list. In Flutter, you can use DropdownButton for simple selections and DropdownButtonFormField when you need to integrate with a Form (validation, saving). Dropdowns are great for choosing from a limited set of options, such as gender, country, or category.
Basic DropdownButton
A DropdownButton requires three main parameters: value (the currently selected item), items (a list of DropdownMenuItem widgets), and onChanged (a callback that receives the new value).
The value must match one of the DropdownMenuItem values, otherwise the dropdown may show nothing or the hint.
Using DropdownMenuItem
DropdownMenuItem is the widget that represents each option. It can contain any widget (e.g., a row with an icon and text).
Customizing Appearance
hint: Placeholder text when no value is selected.
disabledHint: Hint when the dropdown is disabled.
elevation: Elevation of the dropdown menu (shadow).
icon: The icon to the right of the selected item.
iconSize: Size of the dropdown icon.
style: Text style for the selected item.
dropdownColor: Background color of the dropdown menu.
isExpanded: Whether the dropdown should take the full width of its parent.
DropdownButtonFormField for Forms
DropdownButtonFormField is a wrapper that integrates with Form and provides validation and saving. It works similarly to DropdownButton but adds validator and onSaved callbacks.
Dynamic Items from API
You can load dropdown items asynchronously, for example from a REST API. Use FutureBuilder or state management to update the items when data arrives.
Common Mistakes
- Not providing a
valuethat matches an item: Ifvalueis set to a string not initems, the dropdown will showhintor nothing. Ensurevalueis one of the item values.
- Not providing a
- Forgetting to update
valueinonChanged: If you don't callsetStateto update the value, the dropdown won't visually change.
- Forgetting to update
- Not disposing of controllers: If you use
TextEditingControllerinside dropdown (unlikely), dispose them.
- Not disposing of controllers: If you use
- Using
nullasvaluewithout ahint: The dropdown will show an empty line; always provide ahintor make surevalueis non‑null.
- Using
- Mixing
DropdownButtonFormFieldwithFormwithout aGlobalKey: The form won't be able to validate/save.
- Mixing
Best Practices
- Use
DropdownButtonFormFieldwhen you need validation or form integration.
- Use
- Provide a
hintto give the user context.
- Provide a
- For large lists, consider using a searchable dropdown or a modal with a list.
- Use
isExpanded: trueto make the dropdown fill available width.
- Use
- Handle
nullvalues gracefully; you might allow no selection initially.
- Handle
Key Takeaways
DropdownButtonis the basic dropdown widget.
DropdownMenuItemdefines each option; it can contain any widget.
- Use
value,items, andonChangedto control the dropdown.
- Use
- For forms, use
DropdownButtonFormFieldwhich adds validation and saving.
- For forms, use
- Items can be built dynamically from data sources.