【发布时间】:2021-05-13 05:44:59
【问题描述】:
我创建的自定义下拉菜单示例,但无法按图标显示选定的下拉菜单。这里如果我将 isSelected 设置为 true,所有下拉项目都显示图标,但我只希望选定的下拉项目显示图标。图标 im使用的是 Icons.verified_rounded。
有没有办法实现这一点? 下面是自定义小部件的示例代码。
@override
Widget build(BuildContext context) {
return Container(
width: width ,
height: height ,
alignment: Alignment.center,
margin: margin ?? EdgeInsets.symmetric(vertical: 10),
padding: padding ?? EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
color: backGroundColor ?? Colors.white,
border: border ?? Border.all(color: gray),
borderRadius: borderRadius ?? BorderRadius.circular(5)),
child: DropdownButton<String>(
icon: RotatedBox(
quarterTurns: 1,
child: endWidget ??
IconButton(
icon: Icon(
Icons.arrow_forward_ios,
size: 20,
),
onPressed: null)),
iconSize: 18,
hint: Text(
selectedValue ?? 'Select',
style: style ??
TextStyle(
fontSize: 16,
color: gray),
),
isExpanded: true,
underline: const SizedBox(),
onChanged: (String newValue) => onChange(newValue),
items: children.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Row(
children: [
Text(value),
Spacer(),
(isSelected) ? Icon(Icons.verified_rounded) : SizedBox()
],
),
);
}).toList()),
);
}
}
【问题讨论】: