Unverified Commit 9b564e9a authored by xubaolin's avatar xubaolin Committed by GitHub

Make TabBar indicator color automatic adjustment optional (#68171)

parent 5034d575
...@@ -597,6 +597,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -597,6 +597,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
this.controller, this.controller,
this.isScrollable = false, this.isScrollable = false,
this.indicatorColor, this.indicatorColor,
this.automaticIndicatorColorAdjustment = true,
this.indicatorWeight = 2.0, this.indicatorWeight = 2.0,
this.indicatorPadding = EdgeInsets.zero, this.indicatorPadding = EdgeInsets.zero,
this.indicator, this.indicator,
...@@ -685,6 +686,13 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -685,6 +686,13 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
/// the tab widget itself. /// the tab widget itself.
final Decoration? indicator; 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. /// Defines how the selected tab indicator's size is computed.
/// ///
/// The size of the selected tab indicator is defined relative to the /// The size of the selected tab indicator is defined relative to the
...@@ -814,7 +822,11 @@ class _TabBarState extends State<TabBar> { ...@@ -814,7 +822,11 @@ class _TabBarState extends State<TabBar> {
// //
// The material's color might be null (if it's a transparency). In that case // 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. // 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; color = Colors.white;
return UnderlineTabIndicator( return UnderlineTabIndicator(
......
...@@ -169,7 +169,7 @@ class TabControllerFrameState extends State<TabControllerFrame> with SingleTicke ...@@ -169,7 +169,7 @@ class TabControllerFrameState extends State<TabControllerFrame> with SingleTicke
} }
} }
Widget buildLeftRightApp({ required List<String> tabs, required String value }) { Widget buildLeftRightApp({required List<String> tabs, required String value, bool automaticIndicatorColorAdjustment = true}) {
return MaterialApp( return MaterialApp(
theme: ThemeData(platform: TargetPlatform.android), theme: ThemeData(platform: TargetPlatform.android),
home: DefaultTabController( home: DefaultTabController(
...@@ -180,6 +180,7 @@ Widget buildLeftRightApp({ required List<String> tabs, required String value }) ...@@ -180,6 +180,7 @@ Widget buildLeftRightApp({ required List<String> tabs, required String value })
title: const Text('tabs'), title: const Text('tabs'),
bottom: TabBar( bottom: TabBar(
tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(), tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
automaticIndicatorColorAdjustment: automaticIndicatorColorAdjustment,
), ),
), ),
body: const TabBarView( body: const TabBarView(
...@@ -2213,7 +2214,16 @@ void main() { ...@@ -2213,7 +2214,16 @@ void main() {
expect(tabBarBox, paints..line( expect(tabBarBox, paints..line(
color: Colors.white, 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<String> tabs = <String>['LEFT', 'RIGHT'];
await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT', automaticIndicatorColorAdjustment: false));
final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
expect(tabBarBox, paints..line(
color: const Color(0xff2196f3),
));
}); });
testWidgets('Skipping tabs with global key does not crash', (WidgetTester tester) async { testWidgets('Skipping tabs with global key does not crash', (WidgetTester tester) async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment