Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

dart - Set height of DropdownButtonFormField list

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),
          ],
        ),
      )),
);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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",
              ),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...