如何在DropdownButtonFormField中居中显示提示文本



我想将提示文本放在表单字段的中心。我怎样才能做到这一点?

我尝试过使用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: (_) {},
)

由此,我希望文本居中,但结果是这样的:

当前下拉

您必须更改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: (_) {},
)

最新更新