【发布时间】:2020-10-13 23:50:04
【问题描述】:
我想从我的 Flutter Web 应用程序的另一个类的下拉菜单中获取当前选择的值。由于 dropdownValue 变量是本地变量,因此在其类范围之外无法使用。 这是我尝试过的:
class _DPWidgetState extends State < DPWidget > {
String dropdownValue = '1';
@override
Widget build(BuildContext context) {
return DropdownButton < String > (
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: < String > ['1', '2', '3']
.map < DropdownMenuItem < String >> ((String value) {
return DropdownMenuItem < String > (
value: value,
child: Text(value),
);
}).toList(),
);
}
}
【问题讨论】:
-
这个其他类是否需要访问此小部件的选定值父级?
-
@ClaudioRedi:是的,我希望其他类在用户更改下拉菜单时获得更新的值。
标签: flutter drop-down-menu flutter-layout flutter-web