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 {
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<TabBar> {
//
// 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(
......
......@@ -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(
theme: ThemeData(platform: TargetPlatform.android),
home: DefaultTabController(
......@@ -180,6 +180,7 @@ Widget buildLeftRightApp({ required List<String> tabs, required String value })
title: const Text('tabs'),
bottom: TabBar(
tabs: tabs.map<Widget>((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<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 {
......
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