
Hello Friends, Today we are going to learn how to use Autocomplete Widget in Flutter. Actually when I was working on Pincode to City conversion feature for my personal project using Indian Post API, that time there I was needed this feature too. So let’s start without wasting any time and if you want to learn more about Autocomplete Widget please check https://api.flutter.dev/flutter/material/Autocomplete-class.html
main.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Autocomplete Widget Test', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Autocomplete Widget Test'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { List<String> suggestions = [ "Apple", "Banana", "Cucumber", "Doctor", "Everywhere", "Fruits", "Guava", "Honey", "Ice-Cream" "Jack-fruit", "Kite" ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(widget.title)), body: Padding( padding: EdgeInsets.all(12), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ Autocomplete<String>( optionsBuilder: (TextEditingValue textEditingValue) async { return suggestions .where((String val) => val .toLowerCase() .contains(textEditingValue.text.toLowerCase())) .toList(); }, fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) { return TextFormField( key: GlobalKey<FormState>(), controller: fieldTextEditingController, focusNode: fieldFocusNode); }, onSelected: (String selection) { print("${selection}"); }, optionsViewBuilder: (BuildContext context, AutocompleteOnSelected<String> onSelected, Iterable<String> options) { return Align( alignment: Alignment.topLeft, child: Material( child: Container( color: Colors.blue, width: MediaQuery.of(context).size.width - 28, child: ListView.builder( padding: EdgeInsets.all(10.0), itemCount: options.length, itemBuilder: (BuildContext context, int index) { final String option = options.elementAt(index); return GestureDetector( onTap: () { onSelected(option); }, child: ListTile( title: Text("${option}", style: const TextStyle(color: Colors.white)), ), ); }, ), ), ), ); }, ), ], ), ), // This trailing comma makes auto-formatting nicer for build methods. ); } } |
Output
Source Code
https://github.com/rajeshkumarsahanee/autocomplete_widget_test
Thanks for visiting
If you find this helpful then please do share
Comments