Flutter - Autocomplete Widget
Last Updated :
24 Apr, 2025
In this article, we will learn how to develop an autocomplete feature in Flutter that allows users to quickly search and select items from a large set of data. To demonstrate this feature, we will walk through a demo program step-by-step. This functionality can be achieved using the autocomplete widget in Flutter. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Open your VS-code create a Flutter Application And Installing Module using the following steps:
- Open the Command Palette (Ctrl+Shift+P (Cmd+Shift+P on macOS)).
- Select the Flutter: New Project command and press Enter.
Select Application and press Enter.

- Select a Project location.
- Enter your desired Project name.
Now your project structure looks like this:
Step 2: Create a stateless widget by writing the following code inside your main.dart file
Dart
import 'package:flutter/material.dart';
// entry point of your application
void main() {
runApp(const MyApp());
}
// passing this stateless widget
// inside the runApp Method
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
// Material app
return MaterialApp(
title: 'Autocomplete widget Demo',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: const MyHomePage(),
);
}
}
// Stateless widget named as MyHomePage
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
// suggesstion list
static const List<String> _fruitOptions = <String>[
'apple',
'banana',
'orange',
'mango',
'grapes',
'watermelon',
'kiwi',
'strawberry',
'sugarcane',
];
@override
Widget build(BuildContext context) {
// scaffold widget
return Scaffold(
// appbar of your application
appBar: AppBar(
title: const Text("AutoComplete widget"),
),
// column widget inside it we have two widgets
// 1) Text widget 2) Autocomplete widget
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("enter Fruit name:"),
// Auto complete widget
Autocomplete<String>(
optionsBuilder: (TextEditingValue fruitTextEditingValue) {
// if user is input nothing
if (fruitTextEditingValue.text == '') {
return const Iterable<String>.empty();
}
// if user is input something the build
// suggestion based on the user input
return _fruitOptions.where((String option) {
return option
.contains(fruitTextEditingValue.text.toLowerCase());
});
},
// when user click on the suggested
// item this function calls
onSelected: (String value) {
debugPrint('You just selected $value');
},
),
],
),
);
}
}
Now let's understand the complete code:
import 'package:flutter/material.dart';
We import flutter/material.dart package so that we can implement the material design, we can use widgets like stateless widgets, materialApp, etc.
void main() {
runApp(const MyApp());
}
Every flutter code has an entry point from the main function, inside the main function we call the runApp method provided by 'flutter/material.dart' Â package and we pass the widget MyApp as a parameter.
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Autocomplete widget Demo',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: const MyHomePage(),
);
}
}
Now we create our stateless widget MyApp by extending the stateless widget class provided by the material.dart package, then we override the build method implemented in statelessWidget class and return the materialApp widget with the required parameters.
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
static const List<String> _fruitOptions = <String>[
'apple',
'banana',
'orange',
'mango',
'grapes',
'watermelon',
'kiwi',
'strawberry',
'sugarcane',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("AutoComplete widget"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("enter Fruit name:"),
Autocomplete<String>(
optionsBuilder: (TextEditingValue fruitTextEditingValue) {
if (fruitTextEditingValue.text == '') {
return const Iterable<String>.empty();
}
return _fruitOptions.where((String option) {
return option
.contains(fruitTextEditingValue.text.toLowerCase());
});
},
onSelected: (String value) {
debugPrint('You just selected $value');
},
),
],
),
);
}
}
For creating the HomeScreen we write the stateless widget called MyHomePage, inside the MyHomePage we use the Autocomplete Widget that takes OptionsBuilder and OnSelected as parameters.
Step 3: Run the following code by selecting the Android emulator or on your physical device by connecting it.
Output Screenshots:
Â
Output Videos:
Similar Reads
Flutter - BackDropFilter Widget
In this article, we will be going to learn about BackDrop Filter Widget in Flutter and see how to implement it in Flutter. BackDrop Filter widget that applies a filter to the existing painted content to make it blur so as to show the clear text on the screen. The filter will be applied to all the ar
4 min read
Flutter - Custom Widgets
We create Custom Widgets when we want a custom look and feel to our app, and we know that there will be a repetition of a particular widget. We can create the custom widget in a new dart file with all the codes and defining the parameters that we need in the constructor. For more on how to split wid
8 min read
Flutter - Empty Widget
Empty widget is the flutter widget that is mainly used to notify the user about some event, Like if we are fetching the data from the API or From the database, There may be a case when API returns the null at that moment we can show the empty widget. Also If there is no user internet connection we c
4 min read
Flutter - Chip Widget
Chip is a material design widget which comes built-in with flutter. It can simply be described as a compact element holding an icon and text, usually a rounded rectangle in the background. It can serve many purposes, like it can be simply used as a button, representing a user with a CircleAvatar and
4 min read
Flutter - FlutterLogo Widget
FlutterLogo widget is as simple as it sounds, it is just the flutter logo in the form of an icon. This widget also comes built-in with Flutter SDK. This widget can found its use as a placeholder for an image or icon. Below we will see its implementation with all its properties and constructor.Constr
3 min read
Flutter - Flexible Widget
Flexible is a built-in widget in flutter which controls how a child of base flex widgets that are Row, Column, and Flex will fill the space available to it. The Expanded widget in flutter is shorthand of Flexible with the default fit of FlexFit.tight. Flexible widget plays a very important part in m
8 min read
Flutter - CupertinoPicker Widget
The CupertinoPicker widget in Flutter is part of the Cupertino-themed widgets that mimic the design and behavior of iOS widgets. It's specifically designed to create a scrollable list of items, allowing the user to select one item from the list by scrolling or picking it directly. In this article, w
4 min read
Flutter - Card Widget
Card is a built-in widget in Flutter which derives its design from Google's Material Design Library. The functionality of this widget on screen is, that it is a bland space or panel with round corners and a slight elevation on the lower side. It comes with many properties like color, shape, shadow c
4 min read
Flutter - CircleAvatar Widget
CircleAvatar widget comes built-in with the flutter SDK. It is simply a circle in which we can add background color, background image, or just some text. It usually represents a user with his image or with his initials. Although we can make a similar widget from the ground up, this widget comes in h
4 min read
FlatButton Widget in Flutter
FlatButton is the material design widget in a flutter. It is a text label material widget that performs an action when the button is tapped. Let's understand with the help of examples. Disclaimer: As of May 2021 the FlatButton class in flutter is deprecated. TextButton class should be used instead.
3 min read