flutterflutter-webtypeahead

Flutter Web TypAhead does not work when dropdown item is clicked


I am working on a Flutter web app and created a TypeAheadFormField with a onSuggestionSelected function set to add an item to a list, which works only when I use arrow keys and press "Enter" but not when I left-click it using the mouse. I now also notice that the typeahead package on pub.dev does not list web as a supported platform which could be the cause of the issue but would still like to seek a second opinion.

Here's the snippet I'm working on for reference:

TypeAheadFormField(
  textFieldConfiguration:
      TextFieldConfiguration(
          decoration:
              const InputDecoration(
                  border:
                      OutlineInputBorder(
                    borderSide: BorderSide(
                        color:
                            projectsFieldOutline),
                  ),
                  prefixIcon:
                      Icon(Icons.people)),
          controller: collabController),
  suggestionsCallback: (pattern) async {
    List<String> matches =
        collabSuggestions
            .toSet()
            .toList();
    matches.retainWhere((element) =>
        element.toLowerCase().startsWith(
            pattern.toLowerCase()));
    return matches;
  },
  itemBuilder:
      (context, String suggestion) {
    return ListTile(
        title: Text(suggestion));
  },
  onSuggestionSelected:
      (String suggestion) {
    setState(() {
      collabController.text = suggestion;

      print(suggestion);
      collabList
          .add(suggestion.toString());
      print(collabList);
    });
  },
)

Solution

  • To anyone who might face the same issue, I found a workaround that uses a Listener:

    itemBuilder:
        (context, String suggestion) {
      return basic.Listener(
        child: ListTile(
          title: Text(suggestion),
        ),
        onPointerDown: (_) {
          // place logic from onSuggestionSelected here
    
        },
      );
    },