flutter - 如何为 DropDownButtonFormField 设置“选定”项的样式?
问题描述
我的 DropDownButtonFormField 的选定项目与项目列表中的项目不同。
这是我想要做的
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);
},
);
}
}
并像这样显示小部件
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: 30,
width: 200,
child: CurrencyDropDown(),
),
我希望所选值具有与下拉列表中相同的显示,并具有良好的间距和对齐方式。
解决方案
您选择的项目与您的下拉列表不同的原因是因为您的行宽被压缩在您选择的字段中。您可以通过添加您的DropdownButtonFormField
isExpanded: true,
推荐阅读
- xml - SED 多行搜索和替换错误
- github-api - Github API : Github App 在创建问题时应该获得什么权限
- r - 如何生成 2 个具有不同均值的不相关随机正态变量(使用 R)
- webpack-4 - 在 AngularJS1.5.11 中使用 Webpack4 时构建失败
- excel - 如何通过匹配值对Excel中的多列进行排序
- selenium-webdriver - 我试图从硒中调用铬。浏览器正在打开,但没有加载数据
- java - 有时定位服务无法在后台运行
- php - 如何在 WordPress 主题中创建电子商务 html 模板?
- javascript - 如何在 reactjs 中单击按钮时呈现 .js 文件?
- javascript - 我需要它在同一个窗口中打开,而不是在新窗口中打开