【问题标题】:Dropdownbutton restrict shown items下拉按钮限制显示的项目
【发布时间】:2021-04-08 10:44:33
【问题描述】:

我遇到了以下问题: 我希望用户使用下拉按钮选择一个月中的一天。所以我的项目是数字 1 到 31。现在列表变得很长,下拉按钮非常大。是否有解决方案可以显示,例如同时只有 5 个元素?

Widget buildDropdownMonthlyTurnus() {
return DropdownButton<int>(
    value: _selectedDay,
    icon: Icon(Icons.arrow_downward),
    iconSize: 24,
    elevation: 16,
    style: TextStyle(color: Colors.blue),
    underline: Container(
      height: 2,
      color: Colors.blue,
    ),
    onChanged: (int newValue) {
      setState(() {
        _selectedDay = newValue;
      });
    },
    items: Constants.daysOfMonth.map((int value) {
      return new DropdownMenuItem<int>(
        value: value,
        child: new Text(
          value.toString(),
          style: new TextStyle(color: Colors.black),
        ),
      );
    }).toList());

}

在链接中,您可以看到我对大列表的问题。

enter image description here

【问题讨论】:

标签: flutter flutter-widget flutter-dropdownbutton


【解决方案1】:

对于这个问题,你可以使用listview,在一个容器中,根据显示的列表项来管理它的大小,你根本不需要使用下拉项。

【讨论】:

【解决方案2】:

使用 DropdownButton 类的 menuMaxHeight 属性。这是最近添加的用于控制菜单高度的功能。

Widget buildDropdownMonthlyTurnus() {
return DropdownButton<int>(
    menuMaxHeight: 200.0,
    value: _selectedDay,
    icon: Icon(Icons.arrow_downward),
    iconSize: 24,
    elevation: 16,
    style: TextStyle(color: Colors.blue),
    underline: Container(
      height: 2,
      color: Colors.blue,
    ),
    onChanged: (int newValue) {
      setState(() {
        _selectedDay = newValue;
      });
    },
    items: Constants.daysOfMonth.map((int value) {
      return new DropdownMenuItem<int>(
        value: value,
        child: new Text(
          value.toString(),
          style: new TextStyle(color: Colors.black),
        ),
      );
    }).toList());

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多