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:专门用于 定制下拉按钮右侧的图标(通常是 ▼ 箭头)

相关推荐
传奇开心果编程3 小时前
【Flutter踩坑排雷】第7课:巨型 build 函数坑|全部 UI 写在同一个 build,维护难、隐性异常
android·flutter·ios·移动开发·跨平台·dart·声明式ui
程序员老刘6 小时前
从短剧App的bug看响应式编程在AI时代的优势
flutter·ai编程·客户端
恋猫de小郭15 小时前
最近 Android 有什么更新:Android Dev Summit 2026 技术解读
android·前端·flutter
好的佩奇1 天前
Dart 之类修饰符
android·flutter·dart
好的佩奇012 天前
Dart 之类修饰符
android·flutter·dart
Jewel1052 天前
文字为什么总是不居中!记录一个bug
android·flutter·ios
2601_949847752 天前
Form-data 跨端三方库仓颉适配全流程
前端·javascript·flutter
大龄秃头程序员3 天前
Flutter 中我为什么把 GetX 定位为高效率方案,而不是默认架构方案
flutter
传奇开心果编程3 天前
组合优先开发范式:从汉字造字原理到声明式UI组件组合思想
android·flutter·ios·swiftui·移动开发·harmonyos·android jetpack
2501_944521593 天前
Ramda 跨端三方库仓颉适配全流程
开发语言·javascript·flutter·php