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

如何减少Flutter中CheckboxListTile组件之间的间距

缩小CheckboxListTile多选列表间距的解决方案

CheckboxListTile 基于 ListTile 封装实现,默认自带上下内边距、组件最小高度、leading 区域最小占位宽度等默认属性,仅修改复选框的visualDensity无法调整整体选项间距,按如下修改即可:

  • 给 CheckboxListTile 增加 dense: true 开启紧凑型布局
  • 配置 contentPadding 移除默认内边距,可按需设置为EdgeInsets.symmetric(horizontal: 8, vertical: 0)或者EdgeInsets.zero
  • 配置 minLeadingWidth 缩小复选框区域的默认占位宽度,默认值为40,可按需调整到20左右
  • 若需要更精准的高度控制,可给 CheckboxListTile 外层套 SizedBox 固定高度

修改后的完整代码如下:

@override
_CheckBoxInListviewState createState() => _CheckBoxInListviewState();
}

class _CheckBoxInListviewState extends State<CheckBoxInListview> {
  Map<String,bool> _texts = {
    "InduceSmile.com": false,
    "google.com": false,
    "youtube.com": false,
    "yahoo.com": false,
  };
  @override
  Widget build(BuildContext context) {
    return Column(
      children: _texts.keys.map((text) => Theme(
        data: ThemeData(
          checkboxTheme: CheckboxThemeData(
            visualDensity: VisualDensity.compact,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4))
          )
        ),
        child: CheckboxListTile(
          dense: true, // 开启紧凑型布局
          minLeadingWidth: 20, // 缩小复选框区域占位宽度
          contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 0), // 移除默认内边距
          controlAffinity: ListTileControlAffinity.leading,
          title: Text(text, style: TextStyle(fontSize: 13)), // 无需再用Transform做偏移调整
          value: _texts[text],
          onChanged: (val) {
            setState(() {
              _texts[text] = val;
            });
          },
        ),
      )).toList(),
    );
  }
}

如果以上调整还是无法满足间距要求,也可以放弃CheckboxListTile,直接自定义Row组件实现复选框选项,没有默认间距限制灵活度更高:

Row(
  children: [
    Checkbox(
      value: _texts[text],
      onChanged: (val) {
        setState(() {
          _texts[text] = val;
        });
      },
      visualDensity: VisualDensity.compact,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
    ),
    Text(text, style: const TextStyle(fontSize: 13)),
  ]
)

内容的提问来源于stack exchange,提问作者Godwin Alexander Ekainu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 14:39:04