在 Flutter 中,DropdownButtonFormField
的模态列表不断增长以填充一些高度限制,该限制似乎占屏幕的 90%(或者可能,并且更有可能,Container
它在其中)。当它达到该限制时,它变得可滚动。是否可以设置该高度限制?
这是我正在使用的代码
Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Container(
padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
child: Form(
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
//other widgets here
DropdownButtonFormField(items: this.dropDownItems),
],
),
)),
);
Run Code Online (Sandbox Code Playgroud)
我正在检查 的代码DropDown
并且没有设置高度的属性Dialog
,它几乎填满了屏幕。
我对课程做了一个小改动,如果你愿意,你可以将其包含在你的项目中:
https://gist.github.com/tudor07/9f886102f3cb2f69314e159ea10572e1
用法
1- 将文件添加到您的项目中。
2- 使用别名导入文件以避免冲突。
import 'custom_dropdown.dart' as custom;
Run Code Online (Sandbox Code Playgroud)
3- 在与 DropDown 相关的小部件中使用别名,并添加高度属性:
Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Container(
padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
child: Form(
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
//other widgets here
custom.DropdownButtonFormField(
height: 200.0,
items: this.dropDownItems),
],
),
)),
);
Run Code Online (Sandbox Code Playgroud)
4-不要忘记在你DropdownMenuItem
这样的地方添加别名:
custom.DropdownMenuItem(
child: Text("Sample Tex"),
value: "any_value",
),
Run Code Online (Sandbox Code Playgroud)
使用此行,您可以DropdownButtonFormField
像便宜货一样使用:
isDense: false,
itemHeight: 60,//what you need for height
Run Code Online (Sandbox Code Playgroud)
所以你的代码将是这样的:
Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Container(
padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
child: Form(
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
//other widgets here
DropdownButtonFormField(items: this.dropDownItems
isDense: false,
itemHeight: 60,//what you need for height
),
],
),
)),
);
Run Code Online (Sandbox Code Playgroud)
小智 5
我花了30多分钟寻找答案!我只是使用了DropdownButton的“menuMaxHeight”属性
它解决了问题
例如
child: DropdownButton(
menuMaxHeight: 300,
Run Code Online (Sandbox Code Playgroud)
这有效!
归档时间: |
|
查看次数: |
10095 次 |
最近记录: |