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

Flutter中如何减小ExpansionPanel各元素之间的间距

Flutter ExpansionPanel 列表项间距过大调整方案

间距过大来自两处默认配置:

  • ListTile 组件默认遵循Material设计规范的最小高度约束(默认最小高度48px),仅设置contentPadding垂直参数为0无法突破这个最小高度限制
  • ExpansionPanel 及外层ExpansionPanelList默认在header与body之间预留了固定间隙、分隔线占位,body内的Column默认也不会自动做紧凑排布

调整步骤

按以下三点修改即可:

  1. 给所有ListTile外层包SizedBox指定自定义紧凑高度,同时开启dense: true启用组件内置紧凑模式
  2. 给body内的Column设置mainAxisSize: MainAxisSize.min,让列高度完全包裹内容,不预留多余垂直空间
  3. 在外层ExpansionPanelList上设置materialGapSize: 0移除header和body的默认间隙,设置dividerColor: Colors.transparent隐藏分隔线占位

修改后完整代码示例

ExpansionPanelList(
  // 以下两个属性为关键配置,移除panel自带间隙
  materialGapSize: 0,
  dividerColor: Colors.transparent,
  elevation: 0,
  children: [
    ExpansionPanel(
      headerBuilder: (BuildContext context, bool isExpanded) {
        return SizedBox(
          height: 36, // 按需调整头部高度
          child: ListTile(
            title: Text("Files (2)"),
            leading: Icon(Icons.folder_outlined, size: 20),
            minLeadingWidth: 4,
            dense: true,
            contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 16.0),
          ),
        );
      },
      body: Column(
        mainAxisSize: MainAxisSize.min, // 列紧凑排布
        children: [
          SizedBox(
            height: 32, // 按需调整子项高度
            child: ListTile(
              title: Text("File_0"),
              leading: Icon(Icons.insert_drive_file, size: 18),
              minLeadingWidth: 4,
              dense: true,
              // 水平padding设为32做层级缩进,不需要可改回16
              contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 32.0),
            ),
          ),
          SizedBox(
            height: 32,
            child: ListTile(
              title: Text("File_1"),
              leading: Icon(Icons.insert_drive_file, size: 18),
              minLeadingWidth: 4,
              dense: true,
              contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 32.0),
            ),
          )
        ],
      ),
      isExpanded: items[0].isExpanded,
    ),
  ],
)

代码中的高度值、图标大小、缩进值都可以根据实际UI需求自行调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 22:30:57