flutter实践:DropdownButton2使用示例

flutter实现下拉组件

dropdown_button2: '2.3.9'

Dart 复制代码
 @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return DropdownButtonHideUnderline(
      child: DropdownButton2<String>(
        isExpanded: true,
        selectedItemBuilder: (context) {
          return List<Widget>.generate(widget.module.items.length, (index) {
            return _buildSelectedItem(index);
          });
        },
        hint: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              widget.module.selectedValue,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: widget.themeData.fontSizeBase,
                fontWeight: FontWeight.normal,
                color: widget.themeData.colorTextSecondary,
              ),
              overflow: TextOverflow.ellipsis,
            )
          ],
        ),
        items: widget.module.items
            .map((String item) => DropdownMenuItem<String>(
                  value: item,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      _buildDropItem(item),
                      Visibility(
                        visible: widget.module.items.length !=
                            widget.module.items.indexOf(item) + 1,
                        child: Divider(
                          height: 1,
                          thickness: 1,
                          color: widget.themeData.dividerColorBase,
                        ),
                      ),
                    ],
                  ),
                ))
            .toList(),
        value: widget.module.selectedValue,
        onChanged: (value) {
          setState(() {
            widget.module.selectedValue = value ?? '';
            widget.onValueChanged!(widget.module.selectedValue);
          });
        },
        buttonStyleData: ButtonStyleData(
          height: widget.themeData.heightSm,
          width: widget.themeData.heightSm,
          // padding: const EdgeInsets.only(left: 14, right: 14),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(widget.themeData.radiusSm),
            // border: Border.all(
            //   color: Colors.black26,
            // ),
            color: widget.themeData.dividerColorBase,
          ),
          elevation: 1,
        ),
        // iconStyleData: const IconStyleData(
        //   icon: Icon(
        //     Icons.arrow_forward_ios_outlined,
        //   ),
        //   iconSize: 14,
        //   iconEnabledColor: Colors.yellow,
        //   iconDisabledColor: Colors.grey,
        // ),
        iconStyleData: const IconStyleData(
          icon: SizedBox(),
          iconSize: 0,
          // iconEnabledColor: Colors.yellow,
          // iconDisabledColor: Colors.grey,
        ),
        dropdownStyleData: DropdownStyleData(
          maxHeight: widget.themeData.heightXxl * widget.module.items.length,
          width: widget.style == 0 ? widget.themeData.heightXxxl : 80,
          padding: EdgeInsets.zero,
          // scrollPadding: EdgeInsets.zero,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(widget.themeData.radiusLg),
            color: widget.themeData.fillBase,
          ),
          offset:
              widget.style == 0 ? const Offset(-20, -4) : const Offset(-52, -4),
          scrollbarTheme: ScrollbarThemeData(
            radius: const Radius.circular(40),
            thickness: MaterialStateProperty.all(0),
            thumbVisibility: MaterialStateProperty.all(false),
            trackVisibility: MaterialStateProperty.all(false),
          ),
        ),
        menuItemStyleData: MenuItemStyleData(
          height: widget.themeData.heightXxl,
          padding: EdgeInsets.only(
              left: widget.themeData.hSpacingSm,
              right: widget.themeData.hSpacingSm),
        ),
      ),
    );
  }

value:当前选中的值(必须与items中某一项value相等)

onChanged:用户选择某项时的回调

items:下拉选项列表

hint:未选择时显示的提示文本/控件

dropdownStyleData:配置下拉菜单面板样式

buttonStyleData:配置下拉按钮本身的样式

menuItemStyleData:配置每个下拉选项(item)样式

isExpanded:是否撑满父容器宽度

selectedItemBuilder:自定义 下拉按钮中显示的"已选项"内容(即选中后在按钮上显示的 Widget),不影响下拉菜单中的选项显示

iconStyleData:专门用于 定制下拉按钮右侧的图标(通常是 ▼ 箭头)

相关推荐
恋猫de小郭12 小时前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
好的佩奇1 天前
蓝牙之设备角色与连接
android·flutter·蓝牙
SoaringHeart2 天前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
恋猫de小郭3 天前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter
程序员老刘3 天前
跨平台开发地图 | 2026年11月
flutter·ai编程·客户端
传奇开心果编程3 天前
【Flutter踩坑排雷】第30课:异常未捕获坑|未捕获异常直接闪退,全局兜底故障处理
android·flutter·ios·移动开发·dart·ui框架·声明式ui
恋猫de小郭4 天前
Flutter Native Interop 稳定了,Platform Channel 要退场了
android·前端·flutter
好的佩奇4 天前
Dart 之可迭代对象与生成器
android·flutter·dart
恋猫de小郭5 天前
Flutter Analyzer 系列坑推进,必看,AI 时代这玩意最吃你内存
android·前端·flutter
LinXunFeng5 天前
拿竞品 App 一扫,技术栈全暴露了
android·flutter·开源