AbdulRahmanAlHamali / flutter_typeahead

A TypeAhead widget for Flutter, where you can show suggestions to users as they type
BSD 2-Clause "Simplified" License
831 stars 348 forks source link

flutter_typeahead Get the initially selected option #606

Open alejandroalbarracin opened 1 month ago

alejandroalbarracin commented 1 month ago

Hello, can you help me with a question? I have an array where I select a city and save the data in a database, but when I try to modify the city, the previously saved city is not loading correctly. I’m not sure if flutter_typeahead can handle this.

TypeAheadField( //renderiza el texto para que se muestre al momento de seleccionar la ciudad del array controller: cityController, builder: ( BuildContext context, controller, FocusNode focusNode) {

                    return TextField(
                      controller: controller,
                      focusNode: focusNode,
                      obscureText: false,
                      decoration: InputDecoration(
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color:colors.primary, width: 2.0)
                        ),
                        fillColor: Colors.white,
                        border: OutlineInputBorder(

                          borderRadius: BorderRadius.circular(8)
                        ),
                        labelText: 'Buscar y seleccionar ciudad.'
                      ),
                    );
                  },

                  suggestionsCallback: (pattern) async {

                    final Completer<List<City>> completer = Completer();

                    // Cancelar el timer anterior si existe
                    if(_debounce?.isActive ?? false) _debounce!.cancel();
                    // Crear un nuevo timer
                    _debounce = Timer(const Duration(milliseconds: 500), () async {
                     await cityProvider.getCity(limit: 10, page: 1, search: pattern);
                     final filterCity = cityProvider.city.where((city) => 
                      city.nameCity.toLowerCase().contains(pattern.toLowerCase())
                    ).toList(); 

                    completer.complete(filterCity); 

                    });

                    return completer.future;
                  },

                  itemBuilder: (context, City suggestion){
                    return ListTile(
                      title: Text(suggestion.nameCity),
                    );
                  }, 

                  onSelected: (City suggestion){
                    //actualizar el estado con la seleccion seleccionada
                    setState(() {
                      selectCity = suggestion.nameCity;

                      cityId = suggestion.id ;
                      cityController.text = suggestion.nameCity;
                    });

                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('La ciudad seleccionada fue ${suggestion.nameCity}'))
                    );
                    print('seleccion ${suggestion.id}');
                  }, 
venkat9507 commented 1 month ago

can you explain clearly , previous not loading correctly means ?