【发布时间】:2021-11-08 00:04:42
【问题描述】:
我刚开始学习flutter,之前有编程知识,但是刚刚习惯了widget的结构。我想要做的是向 DropdownButton 列表的最后一个元素添加一个按钮,我使用 DropdownMenuItem 小部件来做到这一点。我使用 Text 作为子元素,并将 Button 放在最后一个元素中。问题是,我无法将 Text 赋予 DropdownButton 的 value 属性。这就是我收到错误的原因,因为我给出的值不在项目 [] 中。有什么方法可以获取 Text 小部件的值吗?或者我可以用其他方式吗?我希望我是解释性的。
代码:
class _TodoPageState extends State<TodoPage> {
Text dropdownValue = Text('123'); // **FOR DEFAULT VALUE**
@override
Widget build(BuildContext context) {
return SafeArea(
child: Center(
child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// crossAxisAlignment: CrossAxisAlignment.center,
children: [
MyTabBar(),
Row(
children: [
Expanded(
child: Container(
margin: EdgeInsets.only(left: 3, top: 5),
child: Row(
children: [
Ink(
width: 152,
height: 45,
padding: EdgeInsets.all(6),
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 2),
borderRadius: BorderRadius.circular(10),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue, **// CANT SET THE DEFAULT VALUE**
isExpanded: true,
icon: Image.asset('assets/down-list-arrow.png'),
iconSize: 10,
elevation: 16,
onChanged: (newValue) {
setState(() {
dropdownValue = newValue!; **// CANT SET THE DEFAULT VALUE**
});
},
items: [
DropdownMenuItem(child: Text('123'), value: ''),
DropdownMenuItem(child: Text('123'), value: ''),
DropdownMenuItem(
child: TextButton(
child: Text('Create'),
onPressed: () {},
))
],
),
),
)
],
),
),
),
],
),
MyListView()
],
),
));
}
}
【问题讨论】:
标签: flutter