felixmccuaig / flutter-autocomplete-textfield

An autocomplete Textfield for flutter
MIT License
181 stars 131 forks source link

Dynamic suggestions #76

Closed tezine closed 2 years ago

tezine commented 4 years ago

Hi, is it possible to use a dynamic suggestion list? The idea is to retrieve a suggestion list from the backend based on what's typed by the user, so whenever the text changes, a new list is filled with suggestions. I'm using Consumer and Provider, updating the suggestion list onPointerDown, and calling notifyListeners after the retrieval of new items, but they are not displayed as suggestions. Is there anyway to contour this issue?

rivetingpeppermint commented 4 years ago

I know it's been a month, but this might help. Use a global key assigned to the autocomplete textfield, e.g. _autoKey. Use updateSuggestions, like this: _autoKey.currentState.updateSuggestions(List suggestions);

kizinho commented 3 years ago

can you give complete code sample having same issues

kenle commented 3 years ago
  1. Define a variable to hold your AutoCompleteTextField instance, e.g. AutoCompleteTextField autoComplete;
  2. In Widget build(Build context) set autoComplete variable to AutoCompleteTextField instance
  3. Use the autoComplete.updateSuggestions(suggestions) method whenever suggestions array is updated

That worked for me.

marcosalberto1991 commented 2 years ago

con respuesta muy pobres le voy a pasa la solución definitiva, hay que pensar como no conocemos el código

  1. tiene que añadir el key GlobalKey<AutoCompleteTextFieldState<String>> keyAutoCompleteText = new GlobalKey(); la llave keyAutoCompleteText se declara al inicio del componete
  2. llama el método para actualiza la lista keyAutoCompleteText.currentState.updateSuggestions(listaPlaca); en este caso de uso use un método obtenerListaPlaca() este me actualiza la información y ejecuto el keyAutoCompleteText.currentState.updateSuggestions(listaPlaca);

ejemplo completo:


class InspeccionDiariaVehiculoForm extends StatefulWidget {
  @override
  InspeccionSstRcoFormState createState() => InspeccionSstRcoFormState();
}
class InspeccionSstRcoFormState extends State<InspeccionDiariaVehiculoForm> {
  GlobalKey<AutoCompleteTextFieldState<String>> keyAutoCompleteText = new GlobalKey();

  Future<List<String>> obtenerListaPlaca() async {
    listaPlaca = await vehiculoPlacaDB.getDataList();
    keyAutoCompleteText.currentState.updateSuggestions(listaPlaca);

 }

}

Widget buildTextFielPlaca() {
    return SimpleAutoCompleteTextField(
      controller: controllerPlaca,
      suggestions: listaPlaca,
      decoration: InputDecoration(
        labelText: "Placa",
      ),
      key: keyAutoCompleteText,
      onFocusChanged: (hasFocus) {},
      textSubmitted: (text) {
        //_controller_tipo_unidad.text = text;
      },
      clearOnSubmit: false,
    );
  }
felixmccuaig commented 2 years ago

This can be done, just update the suggestions list when the backend request is completed.