【发布时间】:2021-04-28 00:37:57
【问题描述】:
我是 Flutter 新手。
我在应用中有一个页面(有状态小部件),列中有很多小部件。为了提高代码的可读性,我采用了一些小部件,并将它们制成单独的类。例如,我将下拉菜单小部件放入其唯一的类中,如下所示:
class DropDownMenuWidget extends StatefulWidget {
DropDownMenuWidget({Key? key}) : super(key: key);
@override
_DropDownMenuWidgetState createState() => _DropDownMenuWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _DropDownMenuWidgetState extends State<DropDownMenuWidget> {
String dropdownValue = 'One';
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
underline: Container(
height: 2,
color: Colors.blue,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: MASLULIM
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}
现在,在父类中,我这样显示小部件:
DropDownMenuWidget(),
但是,问题是,当用户单击一个项目时,我只能从 DropDownMenu 类中检索该值,然后调用 setState() 方法。但是,我需要在父类中读取这个值。我怎样才能得到它?
谢谢
【问题讨论】:
标签: flutter dart flutter-widget