Flutter Dropdown List Example

Flutter, Google's UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase, offers a wide range of widgets to create interactive and visually appealing user interfaces. One such widget is the DropdownButton, which allows users to select an item from a list. In this article, we will explore a comprehensive example of implementing a DropdownButton in Flutter, complete with data population and user interaction.

Flutter dropdown list example
Flutter dropdown list example

Before we dive into the example, let's briefly discuss why you might want to use a DropdownButton. Dropdowns are perfect for situations where you want to present a list of options to the user, and allow them to select one. They save space in your UI, as only the selected item is displayed, and the list of options is hidden until the user interacts with the widget.

the job search page for an appliance that allows users to find their jobs
the job search page for an appliance that allows users to find their jobs

Setting Up the DropdownButton Widget

The DropdownButton widget in Flutter is part of the Material library, so you'll need to import it at the top of your Dart file. Here's how you can set up a basic DropdownButton:

a web page with the words dropdown variations
a web page with the words dropdown variations

import 'package:flutter/material.dart';

Creating the DropdownButton

Flutter Project Ideas
Flutter Project Ideas

To create a DropdownButton, you'll need to initialize it with some properties. The most important ones are:

  • value: The currently selected item in the dropdown. Initially, this can be set to null.
  • items: A list of DropdownMenuItem widgets, each representing an option in the dropdown.
  • onChanged: A callback function that is called when the user selects a new item. This function should take the selected item as an argument.

Populating the Dropdown with Data

Handling multi-line items in DropdownButton and DropdownMenuItem · Issue #9211 · flutter/flutter
Handling multi-line items in DropdownButton and DropdownMenuItem · Issue #9211 · flutter/flutter

In many cases, you'll want to populate the DropdownButton with data from a list. You can create a list of strings or any other data type, and then map it to a list of DropdownMenuItem widgets. Here's an example:

String _dropdownValue = 'One';

List<String> _dropdownItems = ['One', 'Two', 'Three', 'Four'];

GitHub - mitesh77/Best-Flutter-UI-Templates: completely free for everyone. Its build-in Flutter Dart.
GitHub - mitesh77/Best-Flutter-UI-Templates: completely free for everyone. Its build-in Flutter Dart.

List<DropdownMenuItem<String>> _dropdownMenuItems = _dropdownItems

.map((String value) {

the cover for flutterr - the complete guide to learn flutterr step - by - step from the ground up
the cover for flutterr - the complete guide to learn flutterr step - by - step from the ground up
Flutter Cheatsheet
Flutter Cheatsheet
Your Flutter Development Roadmap: Master Mobile App Creation 📱
Your Flutter Development Roadmap: Master Mobile App Creation 📱
How To Animate Items In List Using AnimatedList In Flutter
How To Animate Items In List Using AnimatedList In Flutter
Flutter Project Ideas For Beginners
Flutter Project Ideas For Beginners
QuickstersCode - Overview
QuickstersCode - Overview
a piece of paper with the words choose, flutterr if and an arrow pointing to it
a piece of paper with the words choose, flutterr if and an arrow pointing to it
🚀 Flutter – Build Beautiful Apps with One Codebase
🚀 Flutter – Build Beautiful Apps with One Codebase
an image of two cell phones with yellow and black screens next to eachother
an image of two cell phones with yellow and black screens next to eachother
Simple Flutter Patterns
Simple Flutter Patterns
How do you use the typography styles in Flutter?
How do you use the typography styles in Flutter?
Flutter Multiple Selection | Filter ListView Category Using Map Where() & Contain() | Getx
Flutter Multiple Selection | Filter ListView Category Using Map Where() & Contain() | Getx
QuickstersCode - Overview
QuickstersCode - Overview
a pink and blue stuffed animal with hearts on it's chest, wearing a purple scarf
a pink and blue stuffed animal with hearts on it's chest, wearing a purple scarf
QuickstersCode - Overview
QuickstersCode - Overview
Flutter listview implementation
Flutter listview implementation
Learn Flutter Widgets Easily – Complete Beginner Guide
Learn Flutter Widgets Easily – Complete Beginner Guide
flutter ૮ ྀིᴗ͈ . ᴗ͈ ྀིა | happi early valentine <33 //
flutter ૮ ྀིᴗ͈ . ᴗ͈ ྀིა | happi early valentine <33 //
Best Flutter App Examples
Best Flutter App Examples
🚀 Upgrade Your Flutter Game!  Flutter Widget Guide
🚀 Upgrade Your Flutter Game! Flutter Widget Guide

return DropdownMenuItem<String>(

value: value,

child: Text(value),

);

}).toList();

Handling User Interaction

Now that we have set up our DropdownButton and populated it with data, let's look at how to handle user interaction. When the user selects a new item, the onChanged callback is called. Here's how you can update the selected item and display a Snackbar to confirm the user's selection:

void _onDropdownChanged(String newValue) {

setState(() {

_dropdownValue = newValue;

});

ScaffoldMessenger.of(context).showSnackBar(

SnackBar(content: Text('Selected: $_dropdownValue')),

);

}

Using the DropdownButton in a Widget Tree

Finally, let's look at how to use the DropdownButton in a widget tree. Here's an example of how you can include it in a Column widget:

Column({

Key key,

Widget child,

MainAxisSize mainAxisSize = MainAxisSize.max,

MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,

CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,

TextDirection textDirection,

VerticalDirection verticalDirection = VerticalDirection.down,

}) :

super(

key: key,

children: <Widget>[

DropdownButton<String>(

value: _dropdownValue,

items: _dropdownMenuItems,

onChanged: _onDropdownChanged,

),

],

);

In conclusion, the DropdownButton widget in Flutter is a powerful tool for creating interactive and space-efficient user interfaces. By following the example provided in this article, you can set up a DropdownButton, populate it with data, and handle user interaction with ease. Happy coding!