field_suggestion
create highly customizable, simple, and controllable autocomplete fields for flutter.
additional sources
- medium article about fieldsuggestion – autocomplete fields in flutter
installing
see the official installing guidline from – field_suggestion/install
usage & overview
make ready your home widget by creating required options for fieldsuggestion.
final texteditingcontroller = texteditingcontroller();
// and
list<string> suggestionlist = [
'[email protected]',
'[email protected]',
'[email protected]',
];
// or
list<int> numsuggestions = [
13187829696,
13102743803,
15412917703,
];
// or
// note: take look at [class suggestions] part.
list<usermodel> usersuggestions = [
usermodel(email: '[email protected]', username: 'user1', password: '1234567'),
usermodel(email: '[email protected]', username: 'user2', password: 'test123'),
usermodel(email: '[email protected]', username: 'user3', password: 'test123')
];
by resolving #31 we can highlight matchers (suggestions).
so, to do that field suggestion includes special made package highlightable.
however, also you can use it without field suggestion.
see the additional sources part and official repo of highlightable to learn how to use it in field suggestion and also without field suggestion
basic usage.
fieldsuggestion(
textcontroller: texteditingcontroller,
suggestionlist: suggestionlist,
hint: 'email',
),
custom usage.
fieldsuggestion(
textcontroller: secondtextcontroller,
suggestionlist: numsuggestions,
boxcontroller: secondboxcontroller,
onitemselected: (value) {
// do something...
},
fielddecoration: inputdecoration(
hinttext: "phone number",
enabledborder: const outlineinputborder(),
focusedborder: const outlineinputborder(),
),
wdivider: true,
divider: const sizedbox(height: 5),
wslideanimation: true,
slideanimationstyle: slideanimationstyle.ltr,
slidecurve: curves.lineartoeaseout,
animationduration: const duration(milliseconds: 300),
itemstyle: suggestionitemstyle(
leading: const icon(icons.person),
borderradius: const borderradius.all(radius.circular(5)),
boxshadow: [
const boxshadow(
blurradius: 1,
spreadradius: 1,
offset: offset(0, 2),
color: color(0xffd5d5d5),
),
],
),
disableitemtrailing: true,
boxstyle: suggestionboxstyle(
backgroundcolor: colors.white,
borderradius: borderradius.circular(15),
boxshadow: [
boxshadow(
color: colors.blue.withopacity(.2),
spreadradius: 5,
blurradius: 10,
offset: const offset(0, 5),
),
],
),
),
builder
also you can create your own suggestion items by using fieldsuggestion.builder()
.
require to take suggestionlist
, textcontroller
, and itembuilder
.
class builderexample extends statelesswidget {
final texteditingcontroller = texteditingcontroller();
list<string> suggestionslist = ['[email protected]', '[email protected]'];
@override
widget build(buildcontext context) {
return scaffold(
body: fieldsuggestion.builder(
hint: 'email',
textcontroller: texteditingcontroller,
suggestionlist: suggestionslist,
itembuilder: (buildcontext context, int index) {
return gesturedetector(
ontap: () => texteditingcontroller.text = suggestionslist[index],
child: card(
child: listtile(
title: text(suggestionslist[index]),
leading: container(
height: 30,
width: 30,
decoration: boxdecoration(
color: colors.bluegrey,
shape: boxshape.circle,
),
child: center(
child: text(suggestionslist[index][0].touppercase()),
),
),
),
),
);
},
),
);
}
}
external control
here we just wrapped our scaffold
with gesturedetector
to handle gestures on the screen.
and now we can close box when we tap on the screen. (you can do it everywhere, where you used fieldsuggestion
with boxcontroller
).
class example extends statelesswidget {
final _textcontroller = texteditingcontroller();
final _boxcontroller = boxcontroller();
@override
widget build(buildcontext context) {
return gesturedetector(
ontap: () => _boxcontroller.close(),
child: scaffold(
body: center(
child: fieldsuggestion(
hint: 'test',
suggestionlist: [], // your suggestions list here...
boxcontroller: _boxcontroller,
textcontroller: _textcontroller,
),
),
),
);
}
}
class suggestions
usermodel
class, we would use it into suggestionlist
.
note: you must have tojson
method in your model class.
class usermodel {
final string? email;
final string? username;
final string? password;
const usermodel({this.email, this.username, this.password});
// if we wanna use this model class into fieldsuggestion,
// then we must to have tojson method. like that:
map<string, dynamic> tojson() => {
'email': this.email,
'username': this.username,
'password': this.password,
};
}
if we gave a usersuggestions
which is list<usermodel>
.
then we must add the searchby
property. otherwise we will get an error like:
if given suggestionlist's runtimetype isn't list<string>, list<int> or list<double>. that means, you was gave a list which includes dart classes. so then [searchby] can't be null.
our model has email, username, and password, right? so then we can implement it like:
searchby: ['email']
or searchby: ['email', 'username']
.
fieldsuggestion(
hint: 'email',
// if y're using list where are classes,
// don't forget adding search by property.
searchby: ['email', 'username'],
itemtitleby: 'username',
// if you provide [itemsubtitleby] then suggestion
// item's subtitle automaticallty will be enabled.
itemsubtitleby: 'email',
boxcontroller: thirdboxcontroller,
textcontroller: thirdtextcontroller,
suggestionlist: usersuggestions,
onitemselected: (value) {
// the field suggestion needs tojson mehtod inside your model right?
// so that's mean it converts your model to json.
// then the output has to be json (map). so now we can get our value's email.
print(value['passoword']);
},
),
Comments are closed.