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
相关产品推荐
相关产品推荐

