Make TabBar's tab label padding configurable (#21758)
This commit is contained in:
@@ -534,6 +534,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
|
||||
this.indicatorSize,
|
||||
this.labelColor,
|
||||
this.labelStyle,
|
||||
this.labelPadding,
|
||||
this.unselectedLabelColor,
|
||||
this.unselectedLabelStyle,
|
||||
}) : assert(tabs != null),
|
||||
@@ -637,6 +638,11 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
|
||||
/// definition is used.
|
||||
final TextStyle labelStyle;
|
||||
|
||||
/// The padding added to each of the tab labels.
|
||||
///
|
||||
/// If this property is null then kTabLabelPadding is used.
|
||||
final EdgeInsetsGeometry labelPadding;
|
||||
|
||||
/// The text style of the unselected tab labels
|
||||
///
|
||||
/// If this property is null then the [labelStyle] value is used. If [labelStyle]
|
||||
@@ -891,7 +897,7 @@ class _TabBarState extends State<TabBar> {
|
||||
wrappedTabs[i] = Center(
|
||||
heightFactor: 1.0,
|
||||
child: Padding(
|
||||
padding: kTabLabelPadding,
|
||||
padding: widget.labelPadding ?? kTabLabelPadding,
|
||||
child: KeyedSubtree(
|
||||
key: _tabKeys[i],
|
||||
child: widget.tabs[i],
|
||||
|
||||
@@ -1291,6 +1291,76 @@ void main() {
|
||||
));
|
||||
});
|
||||
|
||||
testWidgets('TabBar with labelPadding', (WidgetTester tester) async {
|
||||
const double indicatorWeight = 2.0; // default indicator weight
|
||||
const EdgeInsets labelPadding = EdgeInsets.only(left: 3.0, right: 7.0);
|
||||
const EdgeInsets indicatorPadding = labelPadding;
|
||||
|
||||
final List<Widget> tabs = <Widget>[
|
||||
SizedBox(key: UniqueKey(), width: 130.0, height: 30.0),
|
||||
SizedBox(key: UniqueKey(), width: 140.0, height: 40.0),
|
||||
SizedBox(key: UniqueKey(), width: 150.0, height: 50.0),
|
||||
];
|
||||
|
||||
final TabController controller = TabController(
|
||||
vsync: const TestVSync(),
|
||||
length: tabs.length,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(
|
||||
boilerplate(
|
||||
child: Container(
|
||||
alignment: Alignment.topLeft,
|
||||
child: TabBar(
|
||||
labelPadding: labelPadding,
|
||||
indicatorPadding: labelPadding,
|
||||
isScrollable: true,
|
||||
controller: controller,
|
||||
tabs: tabs,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
|
||||
const double tabBarHeight = 50.0 + indicatorWeight; // 50 = max tab height
|
||||
expect(tabBarBox.size.height, tabBarHeight);
|
||||
|
||||
// Tab0 width = 130, height = 30
|
||||
double tabLeft = labelPadding.left;
|
||||
double tabRight = tabLeft + 130.0;
|
||||
double tabTop = (tabBarHeight - indicatorWeight - 30.0) / 2.0;
|
||||
double tabBottom = tabTop + 30.0;
|
||||
Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
|
||||
expect(tester.getRect(find.byKey(tabs[0].key)), tabRect);
|
||||
|
||||
// Tab1 width = 140, height = 40
|
||||
tabLeft = tabRight + labelPadding.right + labelPadding.left;
|
||||
tabRight = tabLeft + 140.0;
|
||||
tabTop = (tabBarHeight - indicatorWeight - 40.0) / 2.0;
|
||||
tabBottom = tabTop + 40.0;
|
||||
tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
|
||||
expect(tester.getRect(find.byKey(tabs[1].key)), tabRect);
|
||||
|
||||
// Tab2 width = 150, height = 50
|
||||
tabLeft = tabRight + labelPadding.right + labelPadding.left;
|
||||
tabRight = tabLeft + 150.0;
|
||||
tabTop = (tabBarHeight - indicatorWeight - 50.0) / 2.0;
|
||||
tabBottom = tabTop + 50.0;
|
||||
tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
|
||||
expect(tester.getRect(find.byKey(tabs[2].key)), tabRect);
|
||||
|
||||
// Tab 0 selected, indicatorPadding == labelPadding
|
||||
final double indicatorLeft = indicatorPadding.left + indicatorWeight / 2.0;
|
||||
final double indicatorRight = 130.0 + labelPadding.horizontal - indicatorPadding.right - indicatorWeight / 2.0;
|
||||
final double indicatorY = tabBottom + indicatorWeight / 2.0;
|
||||
expect(tabBarBox, paints..line(
|
||||
strokeWidth: indicatorWeight,
|
||||
p1: Offset(indicatorLeft, indicatorY),
|
||||
p2: Offset(indicatorRight, indicatorY),
|
||||
));
|
||||
});
|
||||
|
||||
testWidgets('Overflowing RTL tab bar', (WidgetTester tester) async {
|
||||
final List<Widget> tabs = List<Widget>.filled(100,
|
||||
// For convenience padded width of each tab will equal 100:
|
||||
|
||||
Reference in New Issue
Block a user