Это проблема, которая длилась слишком долго. Я пробовал использовать SimpleAutoComplete или TypeAheadField, но у меня та же проблема.
Простое автозавершение
Вперед
С двумя элементами моя сортировка успешна, но когда я нажимаю на элемент , У меня есть три возможности:
- Плохое состояние: слишком много элементов
- вернуть null в моем элементе
- никогда не вызывать действие
Автозаполнение:
final _nameContributorTextFormFieldDetailsCreateMission = Padding(
padding: EdgeInsets.only(top: 12.0, left: 12.0, bottom: 12.0),
child: SizedBox(
height: 100,
width: 300,
child: SimpleAutocompleteFormField<Employee>(
key: key,
controller: _searchText2Controller,
focusNode: _focusNode,
itemBuilder: (context, contributor) => Padding(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Text(
contributor.lastname,
style: TextStyle(fontWeight: FontWeight.bold),
),
Padding(
padding: EdgeInsets.only(left: 4.0),
child: Text(contributor.firstname),
),
],
),
),
onSearch: (search) async => model.employees
.where((contributor) =>
contributor.firstname
.toLowerCase()
.contains(search.toLowerCase()) ||
contributor.lastname
.toLowerCase()
.contains(search.toLowerCase()))
.toList(),
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nom',
),
itemFromString: (string) => model.employees.singleWhere(
(contributor) =>
contributor.firstname.toLowerCase() == string.toLowerCase(),
orElse: () => null),
onFieldSubmitted: (item) {
print(item);
_searchTextController.text = "${item.firstname}";
},
onSaved: (item) {
print(item);
},
/*onChanged: (Store value) {
model.store = value;
model.setClientOnChangedValue(model.store);
},
onSaved: (Store value) {
model.store = value;
model.setClientOnSavedValue(model.store);
}, */
// validator: (contributor) => contributor == null ? 'Invalid' : null,
),
),
);
TypeAhead:
Padding(
padding: EdgeInsets.only(top: 12.0, left: 12.0, bottom: 12.0),
child: SizedBox(
width: 300,
child: TypeAheadField(
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,
controller: model.typeAheadController,
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: 'Client'),
),
suggestionsCallback: (pattern) async {
print("toto $pattern");
return await model.getSuggestionsClientName(pattern);
},
itemBuilder: (context, suggestion) {
return ListTile(
title: Text(suggestion.toString()),
);
},
transitionBuilder: (context, suggestionsBox, controller) {
print("SuggestionsBox : $suggestionsBox");
print(controller);
return suggestionsBox;
},
onSuggestionSelected: (suggestion) {
print("onclick");
model.typeAheadController.text = suggestion;
},
),
),
);
Или последняя попытка, я умер, мой метод сортировки: (этот метод возвращает значение null)
ListView(
children: <Widget>[
SimpleAutocompleteFormField<Employee>(
decoration: InputDecoration(
labelText: 'Person', border: OutlineInputBorder()),
suggestionsHeight: 80.0,
itemBuilder: (context, person) => Padding(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(person.firstname,
style: TextStyle(fontWeight: FontWeight.bold)),
Text(person.lastname)
]),
),
onSearch: (search) async => model.employees
.where((person) =>
person.firstname
.toLowerCase()
.contains(search.toLowerCase()) ||
person.lastname
.toLowerCase()
.contains(search.toLowerCase()))
.toList(),
itemFromString: (string) => model.employees.singleWhere(
(person) =>
person.firstname.toLowerCase() ==
string.toLowerCase(),
orElse: () => null),
onFieldSubmitted: (item){
print(item);
},
validator: (person) =>
person == null ? 'Invalid person.' : null,
),
],
),