如何让google places建议列表消失,请帮我整理一下这段代码

问题描述 投票:0回答:0

当我从谷歌地图位置自动完成中选择一个地方时,建议列表不会消失,请查看我的代码并在其中建议一些编辑

class FormDetail2 extends StatefulWidget {
 const FormDetail2(
  {Key? key, required this.onNext2, required this.onPrevious1})
  : super(key: key);
  final VoidCallback onNext2;
 final VoidCallback onPrevious1;

  @override
  State<FormDetail2> createState() => _FormDetail2State();
 }
class _FormDetail2State extends State<FormDetail2> {


  final citySearch = TextEditingController();
  final areaSearch = TextEditingController();

  late FocusNode cityFocus;
  late FocusNode areaFocus;

  @override
  void initState() {
    cityFocus = FocusNode();
    areaFocus = FocusNode();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    cityFocus.dispose();
    areaFocus.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final applicationBloc = Provider.of<ApplicationBloc>(context);
    final name = Provider.of<FlatDetail1>(context);
print(name.id1);

return Scaffold(
  body: (applicationBloc.currentLocation == null)
      ? Center(
          child: CircularProgressIndicator(),
        )
      : SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.all(8.0),
                child: TextField(
                  controller: citySearch,
                  autofocus: false,
                  focusNode: cityFocus,
                  decoration: InputDecoration(
                    hintText: 'Enter Location',
                    suffixIcon: Icon(Icons.search),
                  ),
                  onChanged: (value) =>
                      applicationBloc.searchPlacesCity(value),
                ),
              ),
              Stack(children :[
                   if (applicationBloc.searchResultsCity != null &&
                      applicationBloc.searchResultsCity!.isNotEmpty
                     )
                    Container(
                      height: 300,
                      child: ListView.builder(
                        itemCount:
                            applicationBloc.searchResultsCity?.length,
                        itemBuilder: ((context, index) {
                          return ListTile(
                            title: Text(
                              applicationBloc
                                  .searchResultsCity![index].description
                                  .toString(),
                              style: TextStyle(
                                color: Colors.black,
                              ),
                            ),
                            onTap: () async {
                              List<Location> locations =
                                  await locationFromAddress(
                                applicationBloc
                                    .searchResultsCity![index].description
                                    .toString(),
                              );
                              final lat = locations.last.latitude;
                              final cityTapped = applicationBloc
                                  .searchResultsCity![index].description
                                  .toString();

                              setState(() {
                                citySearch.text = cityTapped;

                                applicationBloc.searchResultsCity =[];
                              });
                            },
                          );
                        }),
                      ),
                    ),
                    
                ],
              ),[

在此处输入图片描述] (https://i.stack.imgur.com/fG29Y.jpg)

请建议我对代码进行任何编辑以解决此问题,谢谢。我附上了代码截图输出

flutter dart google-maps listview google-places-api
© www.soinside.com 2019 - 2024. All rights reserved.