You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter:为DropDownMultiSelect组件设置菜单最大高度

解决DropDownMultiSelect菜单过大的问题

方案1:使用组件内置参数(推荐)

如果你使用的是multiselect包的较新版本,DropDownMultiSelect组件本身提供了menuMaxHeight参数,可直接设置菜单最大高度,无需额外修改:

DropDownMultiSelect<Role>(
  options: List.from(state.rolesList ?? []),
  selectedValues: List.from(state.selectedRoles),
  hintStyle: const TextStyle(
    fontSize: 16,
    color: AppColors.gray900Color,
    fontWeight: FontWeight.w400,
  ),
  onChanged: (value) {
    // 你的业务逻辑
  },
  menuItembuilder: (option) {
    return .... // 你的选项构建逻辑
  },
  childBuilder: (selectedValues) {
    return .... // 你的选中项显示逻辑
  },
  icon: const Icon(Icons.keyboard_arrow_down),
  decoration: StylesHelper.adminInputDecoration,
  whenEmpty: 'selectRole'.tr(),
  // 添加该行设置菜单最大高度,数值可按需调整
  menuMaxHeight: 300.0,
),

方案2:适配旧版本(无menuMaxHeight参数)

如果无法升级包版本,可通过Theme组件包裹DropDownMultiSelect,全局设置下拉菜单的最大高度:

Theme(
  data: Theme.of(context).copyWith(
    dropdownMenuTheme: const DropdownMenuThemeData(
      maxHeight: 300.0, // 自定义菜单最大高度
    ),
  ),
  child: DropDownMultiSelect<Role>(
    options: List.from(state.rolesList ?? []),
    selectedValues: List.from(state.selectedRoles),
    hintStyle: const TextStyle(
      fontSize: 16,
      color: AppColors.gray900Color,
      fontWeight: FontWeight.w400,
    ),
    onChanged: (value) {
      // 你的业务逻辑
    },
    menuItembuilder: (option) {
      return ....
    },
    childBuilder: (selectedValues) {
      return ....
    },
    icon: const Icon(Icons.keyboard_arrow_down),
    decoration: StylesHelper.adminInputDecoration,
    whenEmpty: 'selectRole'.tr(),
  ),
)

内容的提问来源于stack exchange,提问作者Rami Dhouib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:36:10