Autocomplete
An autocomplete provides a list of suggestions based on the user’s input and shows typeahead text for the first match.
It is a form-field and can therefore be used in a form.
Preview
CLI
To generate and customize this style:
dart run forui style create autocomplete
Usage
FAutocomplete(...)
FAutocomplete(
controller: FAutocompleteController(vsync: this),
style: (style) => style,
label: const Text('Country'),
description: const Text('Select your country of residence'),
hint: 'Type to search countries',
onChange: (value) => print('Selected country: $value'),
onSaved: (value) => print('Saved country: $value'),
autovalidateMode: AutovalidateMode.onUserInteraction,
builder: (context, styles, child) => child!,
prefixBuilder: (context, style, states) => Icon(FIcons.globe),
suffixBuilder: (context, style, states) => Icon(FIcons.search),
popoverConstraints: const FAutoWidthPortalConstraints(maxHeight: 400),
clearable: (value) => value.text.isNotEmpty,
rightArrowToComplete: true,
initialText: 'Canada',
items: [
'United States',
'Canada',
'Japan',
'United Kingdom',
'Germany',
'France',
'Australia',
],
);
FAutocomplete.builder(...)
FAutocomplete.builder(
filter: (text) async {
const countries = ['United States', 'Canada', 'Japan'];
return countries.where((country) => country.toLowerCase().contains(query.toLowerCase()));
},
contentBuilder: (context, text, suggestions) => [
for (final suggestion in suggestions) FAutocompleteItem(value: suggestion)
],
controller: FAutocompleteController(vsync: this),
style: (style) => style,
label: const Text('Country'),
description: const Text('Select your country of residence'),
hint: 'Type to search countries',
onChange: (value) => print('Selected country: $value'),
onSaved: (value) => print('Saved country: $value'),
autovalidateMode: AutovalidateMode.onUserInteraction,
builder: (context, styles, child) => child!,
prefixBuilder: (context, style, states) => Icon(FIcons.globe),
suffixBuilder: (context, style, states) => Icon(FIcons.search),
popoverConstraints: const FAutoWidthPortalConstraints(maxHeight: 300),
rightArrowToComplete: true,
clearable: (value) => value.text.isNotEmpty,
);
Examples
Detailed
Preview
Sections
Preview
Dividers
Preview
Behavior
Async
Preview
Async with Custom Loading
Preview
Async with Custom Error Handling
Preview
Clearable
Preview
Form
Preview
Last updated on