[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix transparent dividerColor breaks TabBar.tabAlignment
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaTesser committed Jun 17, 2024
1 parent 5187cab commit 66b7844
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
8 changes: 6 additions & 2 deletions packages/flutter/lib/src/material/tabs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1025,13 +1025,17 @@ 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.
final Color? dividerColor;

/// 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.
Expand Down Expand Up @@ -1814,7 +1818,7 @@ class _TabBarState extends State<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;
final bool showDivider = dividerHeight > 0;

tabBar = Align(
heightFactor: 1.0,
Expand All @@ -1826,7 +1830,7 @@ class _TabBarState extends State<TabBar> {
if (showDivider) {
tabBar = CustomPaint(
painter: _DividerPainter(
dividerColor: widget.dividerColor ?? tabBarTheme.dividerColor ?? _defaults.dividerColor!,
dividerColor: dividerColor,
dividerHeight: widget.dividerHeight ?? tabBarTheme.dividerHeight ?? _defaults.dividerHeight!,
),
child: tabBar,
Expand Down
73 changes: 48 additions & 25 deletions packages/flutter/test/material/tabs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6658,7 +6658,7 @@ void main() {
});

// 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 {
testWidgets('Scrollable TabBar with zero divider height does not expand to full width', (WidgetTester tester) async {
Widget buildTabBar({
Color? dividerColor,
double? dividerHeight,
Expand Down Expand Up @@ -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', () {
Expand Down Expand Up @@ -7158,4 +7134,51 @@ void main() {
labelSize = tester.getSize(find.text('Tab 1'));
expect(labelSize, equals(const Size(140.5, 40.0)));
}, skip: isBrowser && !isSkiaWeb); // https://github.com/flutter/flutter/issues/87543

// 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 <Widget>[
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);
});
}

0 comments on commit 66b7844

Please sign in to comment.