Set height of DropdownButtonFormField list

2020-03-24 06:10发布

问题:

In Flutter, DropdownButtonFormField's modal list keeps growing to fill some height limit that seems to be ~90% of the screen (or possibly, and more likely, the Container it's in). When it reaches that limit, it becomes scrollable. Is it possible to set that height limit?

Here's the code I'm working with

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),
          ],
        ),
      )),
);

回答1:

I was checking the code of DropDown and there is no property to set the height of the Dialog, it just fill almost the screen.

I made a small change to the class and you can include to your project if you want:

https://gist.github.com/tudor07/9f886102f3cb2f69314e159ea10572e1

Usage

1- Add the file into your project.

2- Import the file with an alias to avoid conflicts.

 import 'custom_dropdown.dart' as custom;

3- Use the alias in your Widgets related to the DropDown, and add the height property:

    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),
              ],
            ),
          )),
    );

4- Don't forget to add the alias also in your DropdownMenuItem like this :

 custom.DropdownMenuItem(
                child: Text("Sample Tex"),
                value: "any_value",
              ),


标签: dart flutter