如何将提示文本放在DropdownButtonFormField中?

时间:2020-10-06 07:40:09

标签: flutter dart flutter-layout

我想将提示文本放在表单字段的中心。我该如何实现?

我尝试使用Align小部件,但这不起作用。

这是我当前的代码:

List<String> options = [
    'Main room',
    'Side Room',
    'Bathroom(lol)',
    'Back Room',
  ];

DropdownButtonFormField<String>(
   decoration: InputDecoration(enabledBorder: InputBorder.none),
    items: options.map((String value) {
      return DropdownMenuItem<String>(
        value: value,
        child: Align(
            alignment: Alignment.center,
            child: Text(value, style: TextStyle(fontSize: 20))),
      );
    }).toList(),
    isDense: true,
    isExpanded: false,
    hint: Align(
        alignment: Alignment.center,
        child: Text(options[0], style: TextStyle(fontSize: 20))),
    onChanged: (_) {},
  )

据此,我希望文本居中,但结果如下:

Current dropdown

1 个答案:

答案 0 :(得分:0)

您必须更改 isExpanded: true

最终密码:

DropdownButtonFormField<String>(
  decoration: InputDecoration(enabledBorder: InputBorder.none),
  items: options.map((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Align(
          alignment: Alignment.center,
          child: Text(value, style: TextStyle(fontSize: 20))),
    );
  }).toList(),
  isDense: true,
  isExpanded: true,
  hint: Align(
      alignment: Alignment.center,
      child: Text(options[0], style: TextStyle(fontSize: 20))),
  onChanged: (_) {},
)