Git Product home page Git Product logo

Comments (18)

danvick avatar danvick commented on May 14, 2024 1

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.

danvick avatar danvick commented on May 14, 2024 1

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.

JohnKuan avatar JohnKuan commented on May 14, 2024 1

@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.

danvick avatar danvick commented on May 14, 2024

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.

metaltigerfish avatar metaltigerfish commented on May 14, 2024

Would it be possible to implement this from the flutter_typeahead

onSuggestionSelected: (suggestion) {
this._typeAheadController.text = suggestion;
},

from flutter_form_builder.

danvick avatar danvick commented on May 14, 2024

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.

danvick avatar danvick commented on May 14, 2024

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.

metaltigerfish avatar metaltigerfish commented on May 14, 2024

Awesome thank you, I will try that .

from flutter_form_builder.

danvick avatar danvick commented on May 14, 2024

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.

aytunch avatar aytunch commented on May 14, 2024

@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.

aytunch avatar aytunch commented on May 14, 2024

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.

aytunch avatar aytunch commented on May 14, 2024

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.

danvick avatar danvick commented on May 14, 2024

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.

aytunch avatar aytunch commented on May 14, 2024

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.

aytunch avatar aytunch commented on May 14, 2024

@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, ChipInputs keyboard is still open and I can type inside of it.

from flutter_form_builder.

danvick avatar danvick commented on May 14, 2024

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.

aytunch avatar aytunch commented on May 14, 2024

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.

Marcosmaliki avatar Marcosmaliki commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.