Created
July 6, 2020 08:18
-
-
Save erluxman/523818577fa54cb6d0f5e0e8cc1d6a9a to your computer and use it in GitHub Desktop.
Autocomplete Text Field in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_typeahead/flutter_typeahead.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'ShaderMask Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Dough Demo'), | |
), | |
body: FormWidget(), | |
); | |
} | |
} | |
class FormWidget extends StatefulWidget { | |
@override | |
_FormWidgetState createState() => _FormWidgetState(); | |
} | |
class _FormWidgetState extends State<FormWidget> { | |
final TextEditingController input = TextEditingController(); | |
String selected = ""; | |
@override | |
Widget build(BuildContext context) { | |
return Form( | |
child: Column( | |
children: <Widget>[ | |
Image.network( | |
"https://pbs.twimg.com/media/EcOnvabUEAEXY7O?format=jpg&name=medium"), | |
Spacer(flex: 1), | |
Text( | |
"Selected Platform $selected", | |
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700), | |
), | |
Spacer(flex: 1), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
children: <Widget>[ | |
Text('What is your favorite platform?'), | |
TypeAheadFormField( | |
textFieldConfiguration: TextFieldConfiguration( | |
decoration: InputDecoration(labelText: 'Platform'), | |
controller: this.input, | |
), | |
//Search and return found values | |
suggestionsCallback: (pattern) => platforms.where( | |
(item) => | |
item.toLowerCase().contains(pattern.toLowerCase()), | |
), | |
itemBuilder: (_, item) => ListTile(title: Text(item)), | |
onSuggestionSelected: (query) { | |
this.input.text = query; | |
setState(() { | |
selected = query; | |
}); | |
}, | |
), | |
], | |
), | |
), | |
Spacer(flex: 5), | |
], | |
), | |
); | |
} | |
} | |
var platforms = ["Flutter", "Android", "iOS", "Mac", "React", "Cordova"]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment