diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 7ed44d0e9d0..e15d5963a49 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1025,6 +1025,8 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { /// The color of the divider. /// + /// If the [dividerColor] is [Colors.transparent], then the divider will not be drawn. + /// /// If null and [ThemeData.useMaterial3] is false, [TabBarTheme.dividerColor] /// color is used. If that is null and [ThemeData.useMaterial3] is true, /// [ColorScheme.outlineVariant] will be used, otherwise divider will not be drawn. @@ -1032,6 +1034,8 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { /// The height of the divider. /// + /// If the [dividerHeight] is zero or negative, then the divider will not be drawn. + /// /// If null and [ThemeData.useMaterial3] is true, [TabBarTheme.dividerHeight] is used. /// If that is also null and [ThemeData.useMaterial3] is true, 1.0 will be used. /// Otherwise divider will not be drawn. @@ -1813,19 +1817,18 @@ class _TabBarState extends State { 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, + widthFactor: dividerHeight > 0 ? null : 1.0, alignment: effectiveAlignment, child: tabBar, ); - if (showDivider) { + if (dividerColor != Colors.transparent && dividerHeight > 0) { tabBar = CustomPaint( painter: _DividerPainter( - dividerColor: widget.dividerColor ?? tabBarTheme.dividerColor ?? _defaults.dividerColor!, + dividerColor: dividerColor, dividerHeight: widget.dividerHeight ?? tabBarTheme.dividerHeight ?? _defaults.dividerHeight!, ), child: tabBar, diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index a3f6284bf40..74601f061b6 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -6722,30 +6722,6 @@ void main() { 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', () { @@ -7250,4 +7226,51 @@ void main() { expect(find.text('Page 3'), findsOneWidget); expect(scrollable.controller!.position.pixels, equals(0.0)); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/150316. + testWidgets('Scrollable TabBar wuth transparent divider expands to full width', (WidgetTester tester) async { + Widget buildTabBar({ Color? dividerColor, TabAlignment? tabAlignment }) { + return boilerplate( + child: Center( + child: DefaultTabController( + length: 3, + child: TabBar( + dividerColor: dividerColor, + tabAlignment: tabAlignment, + isScrollable: true, + tabs: const [ + Tab(text: 'Tab 1'), + Tab(text: 'Tab 2'), + Tab(text: 'Tab 3'), + ], + ), + ), + ), + ); + } + + // 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, 800.0); + + // 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, 800.0); + + // 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, 800.0); + }); }