diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index dfa3e66275..d6af252a1d 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1693,17 +1693,26 @@ class _TabBarState extends State { TabAlignment.start || TabAlignment.startOffset || TabAlignment.fill => AlignmentDirectional.centerStart, }; - tabBar = CustomPaint( - painter: _DividerPainter( - dividerColor: widget.dividerColor ?? tabBarTheme.dividerColor ?? _defaults.dividerColor!, - dividerHeight: widget.dividerHeight ?? tabBarTheme.dividerHeight ?? _defaults.dividerHeight!, - ), - child: Align( - heightFactor: 1.0, - alignment: effectiveAlignment, - child: tabBar, - ), + final Color dividerColor = widget.dividerColor ?? tabBarTheme.dividerColor ?? _defaults.dividerColor!; + final double dividerHeight = widget.dividerHeight ?? tabBarTheme.dividerHeight ?? _defaults.dividerHeight!; + final bool showDivider = dividerColor != Colors.transparent && dividerHeight > 0; + + tabBar = Align( + heightFactor: 1.0, + widthFactor: showDivider ? null : 1.0, + alignment: effectiveAlignment, + child: tabBar, ); + + if (showDivider) { + tabBar = CustomPaint( + painter: _DividerPainter( + dividerColor: widget.dividerColor ?? tabBarTheme.dividerColor ?? _defaults.dividerColor!, + dividerHeight: widget.dividerHeight ?? tabBarTheme.dividerHeight ?? _defaults.dividerHeight!, + ), + child: tabBar, + ); + } } } else if (widget.padding != null) { tabBar = Padding( diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index f5e4a280ab..536bbf04f1 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -6636,6 +6636,97 @@ void main() { expect(unselectedTextStyle.fontStyle, unselectedLabelStyle.fontStyle); }); + // This is a regression test for https://github.com/flutter/flutter/issues/140338. + testWidgets('Material3 - Scrollable TabBar without a divider does not expand to full width', (WidgetTester tester) async { + Widget buildTabBar({ + Color? dividerColor, + double? dividerHeight, + TabAlignment? tabAlignment, + }) { + return boilerplate( + child: Center( + child: DefaultTabController( + length: 3, + child: TabBar( + dividerColor: dividerColor, + dividerHeight: dividerHeight, + tabAlignment: tabAlignment, + isScrollable: true, + tabs: const [ + Tab(text: 'Tab 1'), + Tab(text: 'Tab 2'), + Tab(text: 'Tab 3'), + ], + ), + ), + ), + ); + } + + // Test default tab bar width when there is a divider and tabAlignment + // is set to startOffset. + await tester.pumpWidget(buildTabBar(tabAlignment: TabAlignment.start)); + expect(tester.getSize(find.byType(TabBar)).width, 800.0); + + // Test default tab bar width when there is a divider and tabAlignment + // is set to start. + await tester.pumpWidget(buildTabBar(tabAlignment: TabAlignment.startOffset)); + expect(tester.getSize(find.byType(TabBar)).width, 800.0); + + // Test default tab bar width when there is a divider and tabAlignment + // tabAlignment is set to center. + await tester.pumpWidget(buildTabBar(tabAlignment: TabAlignment.center)); + expect(tester.getSize(find.byType(TabBar)).width, 800.0); + + // Test default tab bar width when the divider height is set to 0.0 + // and tabAlignment is set to startOffset. + await tester.pumpWidget(buildTabBar( + dividerHeight: 0.0, + tabAlignment: TabAlignment.startOffset, + )); + expect(tester.getSize(find.byType(TabBar)).width, 359.5); + + // Test default tab bar width when the divider height is set to 0.0 + // and tabAlignment is set to start. + await tester.pumpWidget(buildTabBar( + dividerHeight: 0.0, + tabAlignment: TabAlignment.start, + )); + expect(tester.getSize(find.byType(TabBar)).width, 307.5); + + // Test default tab bar width when the divider height is set to 0.0 + // and tabAlignment is set to center. + await tester.pumpWidget(buildTabBar( + dividerHeight: 0.0, + tabAlignment: TabAlignment.center, + )); + expect(tester.getSize(find.byType(TabBar)).width, 307.5); + + // Test default tab bar width when the divider color is set to transparent + // and tabAlignment is set to startOffset. + await tester.pumpWidget(buildTabBar( + dividerColor: Colors.transparent, + tabAlignment: TabAlignment.startOffset, + )); + expect(tester.getSize(find.byType(TabBar)).width, 359.5); + + // Test default tab bar width when the divider color is set to transparent + // and tabAlignment is set to start. + await tester.pumpWidget(buildTabBar( + dividerColor: Colors.transparent, + tabAlignment: TabAlignment.start, + )); + expect(tester.getSize(find.byType(TabBar)).width, 307.5); + + // Test default tab bar width when the divider color is set to transparent + // and tabAlignment is set to center. + await tester.pumpWidget(buildTabBar( + dividerColor: Colors.transparent, + tabAlignment: TabAlignment.center, + )); + expect(tester.getSize(find.byType(TabBar)).width, 307.5); + }); + group('Material 2', () { // These tests are only relevant for Material 2. Once Material 2 // support is deprecated and the APIs are removed, these tests