flutter - Flutter DropdownButtonFormField 不显示在行中
问题描述
我在行内并排添加一个DropdownFormField
和。TextFormField
这就是构建函数的样子
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
DropdownButton(
hint: new Text('Select Gender'),
items: _salutations,
value: client.salutation,
),
SizedBox(width: 20),
new Expanded(
child: TextFormField(
decoration: InputDecoration(labelText: 'Name'),
validator: (value) {
return value.isEmpty ? 'Empty name.' : '';
},
),
),
]
)
],
)
),
);
}
该代码当前显示,因为它是 DropdownButton。但是如果我将它更改为 DropdownButtonFormField 它不会显示,只要它在 Row() 中。为什么是这样?
我希望使用 DropdownButtonFormField 以便 Dropdown 的高度与 TextFormField 相同,因为当前代码显示的下拉列表具有较小的高度。
解决方案
试试这样,
List<String> valueList = ['A', 'B', 'C', 'D'];
String _value = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
height: 50.0,
width: 100.0,
child: new DropdownButtonFormField<String>(
hint: new Text('Select'),
items: valueList.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
),
),
SizedBox(width: 20),
new Expanded(
child: Container(
height: 50.0,
child: TextFormField(
decoration: InputDecoration(labelText: 'Name'),
validator: (value) {
return value.isEmpty ? 'Empty name.' : '';
},
),
),
),
],
)
],
),
),
),
);
}
DropdownButtonFormField
内部必须有一个宽度,Row
因此将其包裹在 a中Container
并width
为此设置Container
希望这可以帮助..
推荐阅读
- pyspark - pyspark 生成特定列的行哈希并将其添加为新列
- c++ - 如何设置 QListWidget 第一项/最后一项的样式?
- c# - 使用标准验证 pt2 导入主数据
- html - 将鼠标悬停在不是后代的工具提示上时保持悬停样式
- swift - 如果 textField 留空,如何恢复 textField 上的“clearsOnBeginEditing(true) 效果”(在开始编辑时删除旧值)?
- ios - Swift:日历应用程序 - 为更多字段着色
- python-2.7 - Python list.append 函数意外更改先前添加的成员
- amazon-web-services - ansible s3_sync 采用本地文件路径而不是远程
- appveyor - 在 AppVeyor Windows 环境中运行的程序的输出不缓冲
- c# - ASP.NET Core MVC 模型验证错误消息未显示