设置 DropdownButtonFormField 列表的高度

mau*_*iii 8 dart flutter

在 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)

die*_*per 9

我正在检查 的代码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)

  • 当我测试时,它没有显示在按钮的中心。它从屏幕顶部显示,然后在指定的高度进行剪切。 (3认同)
  • 不幸的是,这会将带有列表的对话框移动到屏幕顶部,而它应该与 DropDownButton 对齐。 (3认同)
  • 对不起,我应该对那个评论更清楚。成功了,谢谢!但是还有其他行为没有实现到类本身(不是谈论您的自定义)。具体来说,重点功能,奇怪的是这么重要的功能被忽视了。 (2认同)

ali*_*ani 5

使用此行,您可以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)

这有效!