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

Flutter 3.16.4中如何移除可滚动TabBar的左侧内边距?

解决Flutter 3.16.4中可滚动TabBar左侧空白内边距问题

Flutter 3.16.x版本对TabBar的默认布局逻辑做了调整,当设置isScrollable: true时,组件会默认添加左侧水平内边距,导致你遇到的问题。可以通过以下方式解决:

直接设置TabBar的padding为EdgeInsets.zero

在TabBar组件中新增padding属性并设为EdgeInsets.zero,覆盖默认的内边距设置:

Container(
  alignment: Alignment.centerLeft,
  child: TabBar(
    controller: _tabController,
    isScrollable: true,
    padding: EdgeInsets.zero, // 新增该行移除左侧内边距
    labelColor: primaryColor,
    unselectedLabelColor: Colors.grey,
    indicatorColor: Colors.white,
    labelPadding: EdgeInsets.zero,
    physics: const ClampingScrollPhysics(), // to remove bouncing effect on iOS
    tabs: [
      Container(
        color: Colors.red,
        child: Text("Hello"),
      ),
      SizedBox(width: 20),
      Container(
        color: Colors.red,
        child: Text("world"),
      ),
    ],
    onTap: (index) {
      setState(() {}); // just to re render the widget to change the box color
      widget.onTap(index);
    },
  ),
);

全局统一调整(可选)

如果项目中有多个可滚动TabBar需要移除左侧内边距,可以通过全局主题设置统一修改:

ThemeData(
  tabBarTheme: TabBarTheme(
    padding: EdgeInsets.zero, // 全局设置所有TabBar的默认padding
  ),
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 03:00:02