简体   繁体   English

如何为 DropDownButtonFormField 设置“选定”项的样式?

[英]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.

相关问题 如何用Flutter为DropdownButtonFormField的选中项设置正确的alignment? - How to set right alignment for a selected item of DropdownButtonFormField with Flutter? 根据 DropdownButtonFormField flutter 中选择的项目显示表单 - Show form based on item selected in DropdownButtonFormField flutter Flutter DropdownButtonFormField 所选项目未显示 - Flutter DropdownButtonFormField selected item didn't showed up 如何在flutter中设置DropdownSearch Selected Item的样式 - how too style DropdownSearch Selected Item in flutter 删除 DropdownButtonFormField 选中值下划线 - Remove DropdownButtonFormField selected value underline 如果我更改 getx controller 中的列表项并且 dropdownFormField 已从该列表中选择了一个项目,DropdownButtonFormField 会显示错误, - DropdownButtonFormField shows error if I changes the list items in getx controller and dropdownFormField has selected an item from that list, Flutter DropdownButtonFormField 选中背景色 - Flutter DropdownButtonFormField selected background color 如何添加 Flutter DropdownButtonFormField - How to add Flutter DropdownButtonFormField DropdownButtonFormField 不显示选择的值 flutter - DropdownButtonFormField dosn't show the value that selected flutter 如何在 Flutter DropdownButtonFormField 中重置值 - How to reset value in Flutter DropdownButtonFormField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM