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

如何修改Flutter中ExpansionPanelList.radio的图标颜色?

修改Flutter ExpansionPanelList.radio图标颜色的方法

ExpansionPanelList.radio默认的展开/收起图标颜色受Theme相关属性控制,你可以通过以下两种方式修改:

方法一:局部Theme覆盖颜色

在ExpansionPanelList.radio外层包裹Theme组件,自定义图标对应的颜色属性:

Theme(
  data: Theme.of(context).copyWith(
    // 展开状态下的图标颜色
    toggleableActiveColor: Colors.blue,
    // 收起状态下的图标颜色
    unselectedWidgetColor: Colors.white,
  ),
  child: ExpansionPanelList.radio(
    expandedHeaderPadding: const EdgeInsets.only(bottom: 5),
    dividerColor: secondaryColor,
    children: widget.items
        .map(
          (item) => ExpansionPanelRadio(
              canTapOnHeader: true,
              headerBuilder: (context, isExpanded) => ListTile(
                    title: Text(
                      item.name,
                      style: defTextStyle,
                    ),
                  ),
              body: Column(
                children: [],
              ),
              value: item.name),
        )
        .toList(),
  ),
)

方法二:自定义header替换默认图标

如果需要更灵活的样式控制,可以直接在headerBuilder中自定义图标,完全替换默认组件:

ExpansionPanelList.radio(
  expandedHeaderPadding: const EdgeInsets.only(bottom: 5),
  dividerColor: secondaryColor,
  children: widget.items
      .map(
        (item) => ExpansionPanelRadio(
            canTapOnHeader: true,
            headerBuilder: (context, isExpanded) => ListTile(
                  title: Text(
                    item.name,
                    style: defTextStyle,
                  ),
                  trailing: Icon(
                    isExpanded ? Icons.expand_less : Icons.expand_more,
                    color: Colors.white, // 直接设置图标颜色
                  ),
                ),
            body: Column(
              children: [],
            ),
            value: item.name),
      )
      .toList(),
)

注意:使用方法二时,自定义的trailing会覆盖组件默认图标,需要根据isExpanded状态手动切换图标样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 19:45:43