问题描述
我有一个连接到谷歌地点 api 的列表视图,它会随着用户输入地址而更新。当用户单击地址时,我希望将地址放置在文本字段中并关闭列表视图。
下面代码中的selectedItem是用户选择的项目。我正在尝试使用 selectedItem 更新 textformfield。有什么想法吗?
TextFormFiled 和 ListView.builder:
TextFormField(
controller: _controller,decoration: textInputdecoration.copyWith(
labelText: 'Address or Perferred City'),),ListView.builder(
physics: NeverScrollableScrollPhysics(),shrinkWrap: true,itemCount: _placeList.length,itemBuilder: (context,index) {
return ListTile(
title: Text(_placeList[index]["description"]),onTap: () {
selectedItem = _placeList[index]["description"];
print(selectedItem);
});
}),
解决方法
您可以在此处使用
def modify_json_file(json_file):
with open(json_file,"r") as input_json:
json_data = json.load(input_json)
print("type of data: ",type(json_data))
for k,v in json_data.items():
print(k,v)
print('\n'*2)
temperature_settings = json_data["temperature_settings"]
print("type of temperature_settings: ",type(temperature_settings))
print(temperature_settings["temperature_start"])
test_settings = json_data["test_settings"]
temperature_settings["temperature_start"] = 99.9
new_settings = []
new_settings.append(test_settings)
new_settings.append(temperature_settings)
with open(json_file,"w") as input_json:
json_data = json.dump(new_settings,input_json)
print('\n'*25)
modify_json_file("file6.json")```
条件。最初if
是selectedItem
,所以null
是可见的,一旦选择了一个项目,它就不是空的,所以我们隐藏了ListView
。确保在您的 ListView
方法中使用 setState
。一个简单的演示是
onTap