Search code examples
flutterautocompletetypeahead

Show suggestion using TypeAheadField only when user type flutter


I create a textfield that contain suggestion using TypeAheadField packages. I want to show the suggestion only when user type (not when user tap the textField). But as show on the pub [https://pub.dev/packages/flutter_typeahead][1], the example was what Im looking for.

My code :

TypeAheadField(
                                              
                                              textFieldConfiguration:
                                                  TextFieldConfiguration(

                                                autofocus: false,
                                                decoration: InputDecoration(
                                                  border: InputBorder.none,
                                                  focusedBorder:
                                                      InputBorder.none,
                                                  enabledBorder:
                                                      InputBorder.none,
                                                  errorBorder:
                                                      InputBorder.none,
                                                  disabledBorder:
                                                      InputBorder.none,
                                                  hintText: localization
                                                      .insertCustomerName,
                                                ),
                                                controller:
                                                    _typeAheadController,
                                              ),
                                              suggestionsCallback:
                                                  (pattern) async {
                                                return await getSuggestions(
                                                    pattern);
                                              },
                                              hideSuggestionsOnKeyboardHide:
                                                  true,
                                              hideOnEmpty: true,

                                              itemBuilder: (context,
                                                  String suggestion) {
                                                return Padding(
                                                  padding:
                                                      const EdgeInsets.all(
                                                          10.0),
                                                  child: Text(
                                                    suggestion,
                                                  ),
                                                );
                                              },
                                              onSuggestionSelected:
                                                  (String suggestion) {
                                                _typeAheadController.text =
                                                    suggestion;
                                              },

                                            ),

thanks [1]: https://pub.dev/packages/flutter_typeahead


Solution

  • In your suggestionsCallback function, you could try something like this:

    suggestionsCallback: (pattern) async {
      if (pattern != null && pattern.length > 0) {
        return await getSuggestions(pattern);
      } else {
        return [];
      }
    },