In this tutorial, we’ll learn how to properly implement Flutter dropdown search. We’ll be going through a proper code example to practically understand how its done.
Introduction: Flutter Dropdown Search
It specifies a Flutter dropdown widget with a search feature. For instance, we’ve a dropdown of countries and we want to search for a specific country, that’s where search in dropdown plays its role. It saves the user alot of time.
Let’s now see how to implement search in Flutter dropdown with a practical Flutter code example.
Implementing Flutter Dropdown Search
We need to follow the below steps in order to implement Flutter dropdown search in our Flutter app.
Step 1: Import Flutter Dropdown Search Package
dropdown_search: ^5.0.3
Import this dropdown package in your project’s pubspec.yaml file dependencies block and run flutter pub get. Click here to get the latest version.
import 'package:dropdown_search/dropdown_search.dart';
Use this line in the screen where you want to use the dropdown search widget.
Step 2: Creating a List of Items
List countriesList = [ 'Pakistan', 'Afghanistan', 'America', 'China', 'Indonesia' ];
Step 3: Storing the Selected Item in a Variable
String itemSelected = '';
Step 4: Flutter Dropdown Search Widget
DropdownSearch<dynamic>( // we can pass string to it as well but then we've to make // sure that the list of items are string like this List<String> items: countriesList, onChanged: (value) { setState(() { itemSelected = value.toString(); }); }, selectedItem: itemSelected, )
- Items constructor takes a list of strings so we’ll pass the above specified list to it.
- On changed constructor takes a function which is triggered/called whenever an item is selected/tapped in dropdown. The selected value is passed to it as an argument. In our case, we’ve stored the selected value in our string variable.
- Selected item constructor takes a string and its used to show the selected item value in the dropdown button.
popupProps: PopupProps.menu( showSearchBox: true, ),
The complete source code of customized Flutter dropdown search is given in the below section.
Custom Flutter Dropdown Search Source Code
import 'package:dropdown_search/dropdown_search.dart'; import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: HomePage(), ); } } class HomePage extends StatefulWidget { @override State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { List<String> countriesList = [ 'Pakistan', 'Afghanistan', 'America', 'China', 'Indonesia' ]; String itemSelected = ''; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Padding( padding: EdgeInsets.all(20), child: DropdownSearch<String>( items: countriesList, popupProps: PopupProps.menu( showSearchBox: true, ), dropdownButtonProps: DropdownButtonProps(color: Colors.blue,), dropdownDecoratorProps: DropDownDecoratorProps( textAlignVertical: TextAlignVertical.center, dropdownSearchDecoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(50), )), ), onChanged: (value) { setState(() { itemSelected = value.toString(); }); }, selectedItem: itemSelected, ), ))); } }
Conclusion
To conclude this tutorial, hope now you’ve a complete practical knowledge of how to properly implement Flutter dropdown search. We’d be very happy to have your valuable feedback on this post.
We’d also love to see you visit my other tutorials on Flutter app development. Thank you for reading this article.