
微信交流群
# DropdownButtonFormField
DropdownButtonFormField 是一个组合控件,将[DropdownButton]包装在[FormField]中,用法如下:
var _value='语文';
Widget build(BuildContext context) {
return DropdownButtonFormField(
value: _value,
items: [
DropdownMenuItem(
child: Text('语文'),
value: '语文',
),
DropdownMenuItem(child: Text('数学'), value: '数学'),
DropdownMenuItem(child: Text('英语'), value: '英语'),
],
onChanged: (String value){
setState(() {
_value = value;
});
},
);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
selectedItemBuilder
用于自定义选中item的控件,此属性的子控件要和items一一对应,
DropdownButtonFormField(
items: [
DropdownMenuItem(
child: Text('语文'),
value: '语文',
),
DropdownMenuItem(child: Text('数学'), value: '数学'),
DropdownMenuItem(child: Text('英语'), value: '英语'),
],
selectedItemBuilder: (context) {
return [
OutlineButton(child: Text('语文'),onPressed: (){},),
OutlineButton(child: Text('数学'),onPressed: (){},),
OutlineButton(child: Text('英语'),onPressed: (){},),
];
},
...
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
当value为null时,设置提示
DropdownButtonFormField(
hint: Text('请选择'),
value: null,
...
)
1
2
3
4
5
2
3
4
5
decoration
是装饰属性,具体信息查看InputDecoration
onSaved
和validator
是FormField组件的相关参数。
而剩余属性均为DropdownButton属性。
版权所有,禁止私自转发、克隆网站。