Comments (18)
I will consider working on the issue. The problem is that flutter_typeahead
only accepts String
values.
I've had this issue myself in other projects.
My workaround for this issue was to use FormBuilderChipsInput
then set maxChips
to 1
then get the first element using a valueTransformer
. I know that sounds complex, I'll share sample code later on when I have time.
from flutter_form_builder.
Try this out. Let me know if you have any questions.
FormBuilderChipsInput(
decoration:
InputDecoration(labelText: "Contact Name"),
attribute: 'contact',
maxChips: 1, //Allow only one chip
valueTransformer: (val) => val?[0], //Pick the first element from the array
findSuggestions: (String query) async {
List<Contact> contacts = await _fetchContacts();
if (query.length != 0) {
var lowercaseQuery = query.toLowerCase();
return contacts.where((contact) {
return contact.name
.toLowerCase()
.contains(query.toLowerCase());
}).toList(growable: false)
..sort((a, b) => a.name
.toLowerCase()
.indexOf(lowercaseQuery)
.compareTo(b.name
.toLowerCase()
.indexOf(lowercaseQuery)));
} else {
return [];
}
},
chipBuilder: (context, state, contact) {
return InputChip(
key: ObjectKey(contact),
label: Text('${contact.name}'),
onDeleted: () => state.deleteChip(contact),
materialTapTargetSize:
MaterialTapTargetSize.shrinkWrap,
);
},
suggestionBuilder: (context, state, contact) {
return ListTile(
key: ObjectKey(contact),
title: Text('${contact.name}'),
subtitle: Text('${contact.email}'),
onTap: () => state.selectSuggestion(contact),
);
},
),
from flutter_form_builder.
@danvick I have opened a pull request to allow control of the text to update onto TextEditingController. It has an assert statement to check if T is not of type String. Let me know if this works.
https://github.com/danvick/flutter_form_builder/pull/115
from flutter_form_builder.
I think this shows that the suggestionsCallback
is expecting a list of Strings. Try using a class with a toString()
implementation.
Kindly note that flutter_typeahead is a separate package maintained by another developer. Try raising the issue on this repo
from flutter_form_builder.
Would it be possible to implement this from the flutter_typeahead
onSuggestionSelected: (suggestion) {
this._typeAheadController.text = suggestion;
},
from flutter_form_builder.
I get your point @metaltigerfish. However, that would mean giving passing the TextEditingController
for the TypeAhead to the user then it becomes:
onSuggestionSelected: (TextEditingController typeAheadController, dynamic suggestion) {
typeAheadController.text = suggestion.name;
},
Which again seems to make sense. I'll explore the option, though the plan was not to significantly alter the underlying package
from flutter_form_builder.
Meanwhile might I suggest that you map your list of object to a list of strings, I've tried that and it seems to work. Your suggestionsCallback
would end up looking similar to:
suggestionsCallback: (query) {
var contacts = [
AppProfile('Andrew', '[email protected]'),
AppProfile('Brian', '[email protected]'),
AppProfile('Fred', '[email protected]'),
AppProfile('John', '[email protected]'),
AppProfile('Paul', '[email protected]'),
AppProfile('Thomas', '[email protected]')
];
var contactsNames = contacts
.map((contact) => "${contact.name} <${contact.email}>")
.toList(growable: false);
if (query.length != 0) {
var lowercaseQuery = query.toLowerCase();
return contactsNames.where((contact) {
return contact.toLowerCase().contains(lowercaseQuery);
}).toList(growable: false)
..sort((a, b) => a
.toLowerCase()
.indexOf(lowercaseQuery)
.compareTo(b.toLowerCase().indexOf(lowercaseQuery)));
} else {
return contactsNames;
}
},
from flutter_form_builder.
Awesome thank you, I will try that .
from flutter_form_builder.
I'm gonna consider this elegant enough of a solution and close this issue since it doesn't directly relate to this package.
from flutter_form_builder.
@danvick I think your solution helps get rid of the error but inside of the code, we are still not be able to get the whole class object whose name is selected. I opened up an issue as you suggested in flutter_typeahead. Most of the time user will be getting the suggestions from a backend as objects and we would like to process the object(its id for instance) instead of just its .toString() version. Any help would be greatly appreciated. And thanks for this great package.
from flutter_form_builder.
Thats a very interesting workaround @danvick i would love to see the code if possible. Because i have every other module in my form working other than this type ahead and i cant go forward without it. I use google places api to get suggestions. Everything in the form
Looks great. Thanks for this package. Meanwhile I hope author of typeahead spends sometime on this issue as well since its that packages main responsibility to have support for this issue. But again i am ok with a workaround atm since i need to get the suggested object instead of the string to go forward.
from flutter_form_builder.
This look very well and even better for my use case. Thanks @danvick :) However, one thing I could't get to work was constructing the initial suggestion list when query string is empty and the text box is pressed.
I have a list of places which are around the users location and when the user presses the FormBuilderChipsInput
and before types anything I want to show the suggestions. And when he/she types a letter I want to change suggestions. The second part works as of now but the initial suggestions don't work. Do you know how to trigger it?
from flutter_form_builder.
Within findSuggestions
the line that reads:
else {
return [];
}
change it to:
else {
return places;
}
or whatever your equivalent of your code is.
from flutter_form_builder.
Thats how I have it at the moment but it doesn't show the initial suggestions when I touch and gain focus on the textinputview:/
It works only after I type a letter and then backspace. But it doesn't trigger in the beginning.
Is there a way to fire findSuggestions
when the controller gains focus?
from flutter_form_builder.
@danvick Also some part of my suggestion list stays under the keyboard. Is there a way to limit it? Sorry for bothering you this much:/
EDIT: just realized this issue is not present with TypeAhead
but with ChipInput
widget.
EDIT2: ChipInput
widget has a problem with its focus mechanics. When ChipInput
is in focus and for example I select a FormBuilderRadio
item, ChipInput
s keyboard is still open and I can type inside of it.
from flutter_form_builder.
Since I'm the maintainer of ChipsInput
, I'll try to work on the issues you've raised. I may not be able to do it immediately but can I request you to close this issue then open the issues related to ChipsInput
here
from flutter_form_builder.
I will do what you asked for in detail in a couple of hours when i am
In front of my computer. Take care
from flutter_form_builder.
Is it possible to use TypeAhead as an autocomplete textfield? It works great when an item from the list is selected.
I need a way to use the list only as suggestions but also accept new text from the user
from flutter_form_builder.
Related Issues (20)
- [FormBuilderRangeSlider]: Add maxRange value
- No named parameter with the name 'onPopInvoked' HOT 9
- State dos not contain all fields HOT 1
- [Field name or General]: FormBuilderDropdown silently ignores an unknown initialValue
- Resetting form shows error messages HOT 1
- Reuse same widget causes child form field to be overwritten
- [FormBuilderRadioGroup]: No way to decrease padding of Radio widget HOT 4
- [FormBuilderDateTimePicker]: Got problem if initialDate is null and lastDate is years ago.
- [FormBuilderTextField]: Cannot swith to chinese keyborad with keyboardType: TextInputType.text HOT 5
- Internal field 'isDirty' flag is reset too late.
- FormBuilderDropdown onChanged fires twice during form Reset HOT 1
- FormBuilderDropdown when selected keeps triggering a rebuild HOT 1
- [Documentation] RaisedButton is removed.
- [FormBuilderTextField]: TextInputType.emailAddress - not working
- [FormBuilderTextField]: Cannot type the latin text (Vietnamese) on Android HOT 4
- [General]: Add support for lists with multiple fields in each item HOT 1
- [Localizations]: Language code fow Swedish is wrong HOT 1
- [FormBuilderTextField]: Can we add canRequestFocus
- Create custom text field using FormBuilderField
- [FormBuilderField & FormBuilder]: Unexpected retention of `initialValue` when coerced widget recreation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_form_builder.