diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 22a36013d5..8ab94c8c05 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -597,6 +597,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { this.controller, this.isScrollable = false, this.indicatorColor, + this.automaticIndicatorColorAdjustment = true, this.indicatorWeight = 2.0, this.indicatorPadding = EdgeInsets.zero, this.indicator, @@ -685,6 +686,13 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { /// the tab widget itself. final Decoration? indicator; + /// Whether this tab bar should automatically adjust the [indicatorColor]. + /// + /// If [automaticIndicatorColorAdjustment] is true, + /// then the [indicatorColor] will be automatically adjusted to [Colors.white] + /// when the [indicatorColor] is same as [Material.color] of the [Material] parent widget. + final bool automaticIndicatorColorAdjustment; + /// Defines how the selected tab indicator's size is computed. /// /// The size of the selected tab indicator is defined relative to the @@ -814,7 +822,11 @@ class _TabBarState extends State { // // The material's color might be null (if it's a transparency). In that case // there's no good way for us to find out what the color is so we don't. - if (color.value == Material.of(context)?.color?.value) + // + // TODO(xu-baolin): Remove automatic adjustment to white color indicator + // with a better long-term solution. + // https://github.com/flutter/flutter/pull/68171#pullrequestreview-517753917 + if (widget.automaticIndicatorColorAdjustment && color.value == Material.of(context)?.color?.value) color = Colors.white; return UnderlineTabIndicator( diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index bec04f8b69..99d00be089 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -169,7 +169,7 @@ class TabControllerFrameState extends State with SingleTicke } } -Widget buildLeftRightApp({ required List tabs, required String value }) { +Widget buildLeftRightApp({required List tabs, required String value, bool automaticIndicatorColorAdjustment = true}) { return MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: DefaultTabController( @@ -180,6 +180,7 @@ Widget buildLeftRightApp({ required List tabs, required String value }) title: const Text('tabs'), bottom: TabBar( tabs: tabs.map((String tab) => Tab(text: tab)).toList(), + automaticIndicatorColorAdjustment: automaticIndicatorColorAdjustment, ), ), body: const TabBarView( @@ -2213,7 +2214,16 @@ void main() { expect(tabBarBox, paints..line( color: Colors.white, )); + }); + testWidgets('Tab indicator color should not be adjusted when disable [automaticIndicatorColorAdjustment]', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/68077 + final List tabs = ['LEFT', 'RIGHT']; + await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT', automaticIndicatorColorAdjustment: false)); + final RenderBox tabBarBox = tester.firstRenderObject(find.byType(TabBar)); + expect(tabBarBox, paints..line( + color: const Color(0xff2196f3), + )); }); testWidgets('Skipping tabs with global key does not crash', (WidgetTester tester) async {