Flutter Google 地址自动填充

Flutter Google Address Autofill

我想要一个关于如何在 flutter 中添加自动填充地址的最新示例。我已经查看了所有我能找到的资源,有些人把它弄得过于复杂,或者只是复制和粘贴不同的混乱代码。 Atm 我有一个 Google 搜索,它只显示一条蓝线,好像它正在等待并且没有得到响应。

TextFormField(
                controller: addressController,
                onTap: () async {
                  Prediction p = PlacesAutocomplete.show(
                          context: context,
                          apiKey: "MY_API_KEY_SECRET_SHHHHHH",
                          mode: Mode.fullscreen,
                          language: "En",
                          components: [new Component(Component.country, "NZ")])
                      as Prediction;
                  displayPrediction(p);
                },
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return "Customer must have address! cannot be blank";
                  }
                },
                decoration: InputDecoration(
                  hintText: "Enter Customer Address",
                  border: OutlineInputBorder(),
                  prefixIcon: Icon(Icons.map),
                ),
              ),

而且我还有 displayPrediction

Future<Null> displayPrediction(Prediction p) async {
  GoogleMapsPlaces _places = GoogleMapsPlaces(
    apiKey: "API_KEY",
    apiHeaders: await GoogleApiHeaders().getHeaders(),
  );
  PlacesDetailsResponse detail =
      await _places.getDetailsByPlaceId(p.placeId.toString());
  final lat = detail.result.geometry!.location.lat;
  final lng = detail.result.geometry!.location.lng;
}
Broken Search Example

所以我的问题在预测方法中得到了解决。这是我在其他资源的帮助下解决它的方法。

Prediction? p = await PlacesAutocomplete.show(
        context: context,
        apiKey: kGoogleApiKey,
        radius: 10000000,
        types: [],
        strictbounds: false,
        mode: Mode.overlay,
        decoration: InputDecoration(
          hintText: 'Search',
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(20),
            borderSide: BorderSide(
              color: Colors.white,
            ),
          ),
        ),
        components: [Component(Component.country, "nz")],
      );