Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Exploring Simple Widgets II: Autocomplete
Alpha
Alpha

Posted on

     

Exploring Simple Widgets II: Autocomplete

Making user-friendly and effective interfaces is crucial in today's quickly changing world of app development. The Flutter autocomplete widget is one of the widgets that have the potential to substantially improve the user experience. This dynamic feature makes entering and retrieving data easier while enhancing user involvement. This post will explore Flutter's autocomplete widget and show how it can improve how users experience your apps.

The Autocomplete widget is a powerful widget provided by the Flutter library that enables users to efficiently input data by offering predictive suggestions. This functionality significantly reduces user effort and enhances their experience. There are use cases for this widget in E-commerce apps where users can get real-time suggestion when searching for a product, travel and booking apps to suggest places to users and much more.

How to use the Autocomplete Widget

In your scaffold body, we create the Autocomplete widget and provide the requiredoptionsBuilder parameter which is used to provide the list of suggestions that should be displayed when the field is focused. The autocomplete widget infers a type T that extends Object and as such should be created with the type which could be int, String or even custom Objects.
The stripped down code of the autocomplete widget would look like so

 @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text('Autocomplete'),      ),      body: Padding(        padding: const EdgeInsets.all(18.0),        child: Center(          child: Autocomplete<int>(            optionsBuilder: (val){              return [1,2,3,4,5];            },          )        ),      )       );       }
Enter fullscreen modeExit fullscreen mode

Automplete<int>

 @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text('Autocomplete'),      ),      body: Padding(        padding: const EdgeInsets.all(18.0),        child: Center(          child: Autocomplete<String>(            optionsBuilder: (val){              return ['Cat', 'Dog', 'Fish', 'Goat', 'Sheep'];            },          )        ),      )       );       }
Enter fullscreen modeExit fullscreen mode

Autocomplete<String>

The above implementation shows the suggestions as soon as the textfield is in focus but that isn't what we aim to achieve. There are other parameters that gives us customization and allow us to tweak the widget to our need.
To begin, we can customize the widget to only show the options as soon as the user starts typing and show only relevant options based on user input

class Home extends StatelessWidget {  List<String> animals = ['Cat', 'Dog', 'Fish', 'Goat', 'Sheep'];  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text('Autocomplete'),      ),      body: Padding(        padding: const EdgeInsets.all(18.0),        child: Center(          child: Autocomplete<String>(            optionsBuilder: (val){              if(val.text.isEmpty){                return [];              }              return animals.where((animal){                return animal.toLowerCase().contains(val.text);              });            },          )        ),      )       );       }}
Enter fullscreen modeExit fullscreen mode

Autocomplete gif

Customizing Options

Using theoptionsViewBuilder parameter, we can customize the way our suggestions are presented to fit our app's design.

 Autocomplete<String>(            optionsBuilder: (val){              if(val.text.isEmpty){                return [];              }              return animals.where((animal){                return animal.toLowerCase().contains(val.text.toLowerCase());              });            },            optionsViewBuilder: (context,onSelected, options){             return ListView.builder(                    itemCount: options.length,                    itemBuilder: (context,index){                            String x = options.elementAt(index);                      return Card(child: ListTile(                        onTap: () => onSelected(x),                        title: Text(x, style: TextStyle(color: Colors.blue))));                    },                  );            },          )
Enter fullscreen modeExit fullscreen mode

Customized options

The autocomplete widget can be populated with various customized list and even data fetched asynchronously opening various possibilities to the usage. There are also various packages that have been made for even more customization and flexibility to save you a ton of stress. Find them onPub.dev

Conclusion

Autocomplete widgets shine as tools that not only enhance efficiency but also contribute to the overall satisfaction of users. From their seamless implementation to their potential across various industries, autocomplete widgets empower developers to create highly interactive, top-tier applications.
For more information, check out theFlutter docs for the autocomplete class

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am Alpha! I want to tour the world on the wings of flutter!
  • Location
    Nigeria
  • Education
    Federal University of Agriculture, Abeokuta, NIgeria
  • Work
    Flutter Developer, Leisure Trove
  • Joined

More fromAlpha

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp