首页 > 解决方案 > 如何在颤振中添加自动填充下拉菜单?

问题描述

我已经做了一个城市的下拉列表,城市列表来自 API。当用户点击 GPS 按钮时,它会自动填充下拉字段。我应该如何在下拉列表中自动填写城市名称?我尝试在文本字段中使用下拉菜单

TextFormField(
                          controller: city,
                      decoration:InputDecoration(
                         border:InputBorder.none,
                          prefix: DropdownButtonFormField<String>(
                         
                              decoration: InputDecoration.collapsed(
                                  hintText: 'select',
                                  hintStyle: TextStyle(fontSize: 12,color: Colors.grey.shade600),
                                ),
                                value:type,
                                validator: (value) => value == null? 'please select': null,
                                onChanged: (String newValue) {
                                  setState(() {
                                    type = newValue;
                         
                                  });
                                },
                                items: List?.map((item){
                                  return DropdownMenuItem(
                                    onTap: (){
                                      selectedCity=item['city'];
                                      // print(selectedCity);
                                    },
                                    value: item['id'].toString(),
                                    child: Padding(
                                      padding: const EdgeInsets.only(left:15.0,top: 13.0),
                                      child: Text(item['city'],style: TextStyle(fontSize: 12,color: Colors.grey.shade600),),
                                    ),
                                  );
                                })?.toList()??[]
                            ),
                      ),
                      ),

标签: flutterdropdowntextfieldautofill

解决方案


您可以使用现成的自动完成包。

一些例子:

https://pub.dev/packages/autocomplete_textfield

https://pub.dev/packages/dropdownfield

在此处输入图像描述


推荐阅读