jifalops / simple_autocomplete_formfield

A Flutter widget that wraps a TextFormField and assists with autocomplete functionality.
MIT License
42 stars 16 forks source link

Please Put Clear Instructions On how to use #2

Closed kush-singh-chb closed 6 years ago

kush-singh-chb commented 6 years ago

I have been wanting to use your library and cant figure out how to pass it the list of Strings Can you please Help me out with it thanks in advance

jifalops commented 6 years ago

Since each item may have a complex UI, you create the items using the itemBuilder callback.

Say you have a class such as

class Person {
  String name;
  String address;
}

Then to show a list of people you would use

// In widget building function...
// Assume `people` is a `List<Person>`.
SimpleAutocompleteFormField<Person>(
  itemBuilder: (context, person) => 
      Column(children: [ Text(person.name), Text(person.address) ]),
  onSearch: (search) async => people.where((person) => 
      person.name.contains(search) || person.address.contains(search)),
)

The itemParser field is not always necessary but is how the text field is filled in when an item is tapped on. If omitted, it just uses the object's toString() method. itemParser.fromString() is how you would decide which Person was selected based on the value in the text field (if they typed in the person's whole name but never tapped an autocomplete suggestion).

jifalops commented 6 years ago

See the updated example.