简体   繁体   中英

Flutter google places autocomplete on text form field

I want a text form field with flutter google places autocomplete, that when the user select the location, the text will go to the text form field.

I have this code where the user clicks on the text form field, on tap, open the search address bar, but the text does not show on the text form field after selecting the address.

Code

TextFormField(
                          decoration: textInputDecoration.copyWith(
                            hintText: "Enter Company Address",
                            prefixIcon: Icon(Icons.map),
                          ),
                          onTap: () async {
                            Prediction p = await PlacesAutocomplete.show(
                              context: context,
                              radius: 10000000,
                              types: [],
                              strictbounds: false,
                              apiKey: kGoogleApiKey,
                              mode: Mode.overlay,
                              language: "en",
                              components: [Component(Component.country, "nz")],
                            );
                          },
                          validator: (val) =>
                              val.isEmpty ? 'Enter address' : null,
                          onChanged: (val) async {
                            setState(() => address = val);
                          },
                        ),

First, you have to create a TextEditingController _place = TextEditingController()

Then, when the result comes back from the API, for example, components[0] , You assign this String value to the `controller.

_place.text = result . It'll change the UI without using setState even.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM