用于下拉选择的按钮,可指定图标、影深、提示等属性,接收选中变化的事件。
相关组件
DropdownButton基本用法
<br />【value】 : 当前值 【T】<br />【items】 : 下拉选框 【List<DropdownMenuItem<T>>】<br />【icon】 : 图标 【Widget】<br />【elevation】 : 影深 【double】<br />【onChanged】 : 选择条目事件 【Function(T)】<br />【backgroundColor】 : 背景色 【Color】<br />![94.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589446312315-8758cdd5-88a3-4beb-8955-116a23f47c41.gif#align=left&display=inline&height=189&margin=%5Bobject%20Object%5D&name=94.gif&originHeight=189&originWidth=404&size=206429&status=done&style=none&width=404)
import 'package:flutter/material.dart';
class CustomDropDownButton extends StatefulWidget {
@override
_CustomDropDownButtonState createState() => _CustomDropDownButtonState();
}
class _CustomDropDownButtonState extends State<CustomDropDownButton> {
Color _color = Colors.red;
final _colors = [Colors.red, Colors.yellow, Colors.blue, Colors.green];
final _info = ["红色", "黄色", "蓝色", "绿色"];
@override
Widget build(BuildContext context) {
return Wrap(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 20),
width: 50,
height: 50,
color: _color,
),
DropdownButton<Color>(
value: _color,
elevation: 1,
icon: Icon(
Icons.expand_more,
size: 20,
color: _color,
),
items: _buildItems(),
onChanged: (v) => setState(() => _color = v)),
],
);
}
List<DropdownMenuItem<Color>> _buildItems() => _colors
.map((e) => DropdownMenuItem<Color>(
value: e,
child: Text(
_info[_colors.indexOf(e)],
style: TextStyle(color: e),
)))
.toList();
}
DropdownButton的样式指定
<br />【isDense】 : 是否紧排 【bool】<br />【iconSize】 : 图标大小 【double】<br />【hint】 : 提示组件 【Widget】<br />【iconEnabledColor】 : 图标颜色 【Color】<br />![95.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589446397267-ad3a4e4b-0035-4aee-8062-2fc2feec6dd6.gif#align=left&display=inline&height=337&margin=%5Bobject%20Object%5D&name=95.gif&originHeight=337&originWidth=404&size=341959&status=done&style=none&width=404)
import 'package:flutter/material.dart';
class StyleDropDownButton extends StatefulWidget {
@override
_StyleDropDownButtonState createState() => _StyleDropDownButtonState();
}
class _StyleDropDownButtonState extends State<StyleDropDownButton> {
Color _color = Colors.red ;
final _colors = [Colors.red, Colors.yellow, Colors.blue, Colors.green];
final _info = ["红色", "黄色", "蓝色", "绿色"];
@override
Widget build(BuildContext context) {
return Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 20),
width: 50,
height: 50,
color: _color??Colors.transparent,
),
DropdownButton<Color>(
hint: Text('请选择'),
isDense: true,
iconSize:20,
iconEnabledColor:_color??Colors.orange,
value: _color,
items: _buildItems(),
onChanged: (v) => setState(() => _color = v)),
],
);
}
List<DropdownMenuItem<Color>> _buildItems() => _colors
.map((e) => DropdownMenuItem<Color>(
value: e,
child: Text(
_info[_colors.indexOf(e)],
style: TextStyle(color: e),
)))
.toList();
}