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

Flutter中设置Padding时如何移除ExpansionTile展开后的分割线?

解决Flutter中带Padding的ExpansionTile移除展开后分割线的问题

嘿,我懂你的需求:给ExpansionTile加了外层Padding,但想去掉它展开后出现的那条分割线对吧?其实操作很简单,咱们直接针对ExpansionTile本身的属性调整就行,完全不用动你原来的Padding设置。

ExpansionTile默认在展开状态下,标题下方会显示一条分割线,咱们只需要给它设置dividerColor为透明色,就能把这条线彻底隐藏掉,同时外层的Padding效果完全不受影响。

下面是修改后的完整代码:

return Card(
  borderOnForeground: false,
  key: Key("${e.id}"),
  child: Padding(
    padding: EdgeInsets.only(bottom: 8.0),
    child: ExpansionTile(
      // 添加这一行,将分割线设为透明
      dividerColor: Colors.transparent,
      backgroundColor: Theme.of(context).accentColor,
      title: Text(
        e.title,
        style: Theme.of(context).textTheme.bodyText1,
        maxLines: 1,
      ),
      subtitle: Text(e.by),
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Text("${e.descendants} comments"),
            Icon(Icons.open_in_new)
          ],
        )
      ],
    ),
  ),
);

如果你的项目里有多个ExpansionTile都需要去掉分割线,也可以用Theme组件包裹来全局修改,这样就不用每个组件单独设置了:

return Card(
  borderOnForeground: false,
  key: Key("${e.id}"),
  child: Padding(
    padding: EdgeInsets.only(bottom: 8.0),
    child: Theme(
      data: Theme.of(context).copyWith(
        dividerColor: Colors.transparent,
      ),
      child: ExpansionTile(
        backgroundColor: Theme.of(context).accentColor,
        title: Text(
          e.title,
          style: Theme.of(context).textTheme.bodyText1,
          maxLines: 1,
        ),
        subtitle: Text(e.by),
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              Text("${e.descendants} comments"),
              Icon(Icons.open_in_new)
            ],
          )
        ],
      ),
    ),
  ),
);

这两种方法都能完美保留你原来的Padding布局,同时移除展开后的分割线,选哪种看你的实际场景需求就好~

内容的提问来源于stack exchange,提问作者Hoang Trung Nguyen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 19:59:07