[英]How to style the “selected” item for a DropDownButtonFormField?
The selected item of my DropDownButtonFormField is different than the ones in the item list.我的 DropDownButtonFormField 的选定项目与项目列表中的项目不同。
Here is what I am trying to do这是我想要做的
class CurrencyDropDown extends StatefulWidget {
const CurrencyDropDown({
Key key,
}) : super(key: key);
@override
_CurrencyDropDownState createState() => _CurrencyDropDownState();
}
class _CurrencyDropDownState extends State<CurrencyDropDown> {
String selected = "EUR";
@override
Widget build(BuildContext context) {
return DropdownButtonFormField<String>(
value: selected,
hint: new Text("Select your currency...", textAlign: TextAlign.center),
items: ["USD", "EUR", "CHF"]
.map((label) => DropdownMenuItem(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Image.asset(
'icons/currency/${label.toLowerCase()}.png',
package: 'currency_icons',
width: 30,
),
Text(label),
],
),
value: label,
))
.toList(),
onChanged: (value) {
setState(() => selected = value);
},
);
}
}
and displaying the widget like this并像这样显示小部件
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: 30,
width: 200,
child: CurrencyDropDown(),
),
And here is how it looks when selecting这是选择时的外观
and displaying the selection.并显示选择。
I would like the selected value to have the same display like in the dropdown list, with nice spacing and alignment.我希望所选值具有与下拉列表中相同的显示,具有良好的间距和 alignment。
The reason why your selected item is different to your dropdown is because your row width is condensed in your selected field.您选择的项目与您的下拉列表不同的原因是因为您的行宽被压缩在您选择的字段中。 You can fix it by adding in your
DropdownButtonFormField
您可以通过添加
DropdownButtonFormField
来修复它
isExpanded: true,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.