Skip to content

Instantly share code, notes, and snippets.

@kirshiyin89
Last active March 20, 2023 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kirshiyin89/2944e1aa5211df6dd97823ff547abf9f to your computer and use it in GitHub Desktop.
Save kirshiyin89/2944e1aa5211df6dd97823ff547abf9f to your computer and use it in GitHub Desktop.
MaterialApp class.
import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'data.dart';
class MyMaterialApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Datamuse Autocomplete Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Autocomplete'),
),
body: NavigationExample());
}
}
class NavigationExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(32.0),
child: Column(
children: <Widget>[
SizedBox(
height: 10.0,
),
TypeAheadField(
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,
style: DefaultTextStyle.of(context)
.style
.copyWith(fontStyle: FontStyle.italic),
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'What is on your mind?'),
),
suggestionsCallback: (pattern) async {
return await BackendService.getSuggestions(pattern);
},
itemBuilder: (context, Map<String, String> suggestion) {
return ListTile(
title: Text(suggestion['name']!),
subtitle: Text('${suggestion['score']}'),
);
},
onSuggestionSelected: (Map<String, String> suggestion) {
// your implementation here
},
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment