【发布时间】:2019-07-01 12:16:28
【问题描述】:
整页代码很长,但我的 DropdownButton 小部件代码是这样的。
问题是,
首先:我无法更新我的 selectedCity,它没有得到更新。此外,打印函数调用 null,因为我的 cityList 数据类似于 [new york, paris, london] 等...
second:flutter 不会将焦点从任何 TextField 完全更改为 DropdownButton。我的意思是,单击 TextField,然后单击 DropdownButton,但在单击按钮后焦点恢复到该 TextField。是 Flutter 的默认动作吗?
List<dynamic> _cityList;
String _selectedCity;
@override
Widget build(BuildContext context) {
return DropdownButton(
value: _selectedCity,
style: TextStyle(
fontSize: 11,
color: textColor,
),
items: _cityList.map((city) {
return DropdownMenuItem<String>(
child: Padding(
padding: const EdgeInsets.only(left: 4),
child: Text(city),
),
);
}).toList(),
onChanged: (String value) {
setState(() {
_selectedCity = value;
print(_selectedCity);
});
},
isExpanded: true,
);
}
编辑:从 DropdownMenuItem 中选择项目后重置 FocusNode 的解决方案是在 setstate 中添加这一行,如下所示:
这个:FocusScope.of(context).requestFocus(new FocusNode());
到这里:onChanged:(){setSate((){here}}
【问题讨论】:
-
您的解决方案修复了我在过去 3 小时内一直在寻找的东西。谢谢。