import 'package:flutter/material.dart';
import 'dart:ui';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LearnDropdownButton(),
);
}
}
class LearnDropdownButton extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _LearnDropdownButton();
}
}
class _LearnDropdownButton extends State<LearnDropdownButton> {
List<DropdownMenuItem> getListData() {
List<DropdownMenuItem> items = List();
DropdownMenuItem dropdownMenuItem1 = DropdownMenuItem(
child: Text('1'),
value: '1',
);
items.add(dropdownMenuItem1);
DropdownMenuItem dropdownMenuItem2 = DropdownMenuItem(
child: Text('2'),
value: '2',
);
items.add(dropdownMenuItem2);
DropdownMenuItem dropdownMenuItem3 = DropdownMenuItem(
child: Text('3'),
value: '3',
);
items.add(dropdownMenuItem3);
return items;
}
var value;
/* _LearnDropdownButton(){
value=getListData()[0].value;
}*/
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
Center(
child: DropdownButton(
items: getListData(),
hint: Text('请选择:'), //当没有默认值的时候可以设置的提示
value: value, //下拉菜单选择完之后显示给用户的值
onChanged: (T) {
//下拉菜单item点击之后的回调
setState(() {
value = T;
});
},
elevation: 10, //设置阴影的高度
style: TextStyle(
//设置文本框里面文字的样式
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.red),
// isDense: false,//减少按钮的高度。默认情况下,此按钮的高度与其菜单项的高度相同。如果isDense为true,则按钮的高度减少约一半。 这个当按钮嵌入添加的容器中时,非常有用
iconSize: 70.0, //设置三角标icon的大小
),
),
],
),
);
}
}