简体   繁体   English

Flutter 谷歌在文本表单字段上放置自动完成功能

[英]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.我想要一个带有 flutter google 地方自动完成的文本表单字段,即当用户 select 的位置时,文本将 go 到文本表单字段。

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()首先,您必须创建一个TextEditingController _place = TextEditingController()

Then, when the result comes back from the API, for example, components[0] , You assign this String value to the `controller.然后,当结果从 API 返回时,例如components[0] ,您将此String值分配给 `controller.

_place.text = result . _place.text = result It'll change the UI without using setState even.它甚至会在不使用 setState 的情况下更改 UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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