Skip to content

Commit 149be41

Browse files
[material_ui] Fix TabBar assertion to allow indicatorWeight: 0 with a themed indicator (flutter#12696)
This is the port of this [PR](flutter/flutter#189157) after decoupling. Fixes flutter/flutter#188837. ## Original PR Description This PR moves the assertion that checks indicator != null || (indicatorWeight > 0.0) from the TabBar constructors into the Decoration _getIndicator(TabBarIndicatorSize indicatorSize) method. During the first phase of creating the TabBar widget, Dart just fills in the fields, there is no BuildContext available yet, so the widget has no way of access the TabBarTheme via context.dependOnInheritedWidgetOfExactType<TabBarTheme>(). As a result, the constructor's assertion can only see the indicator passed directly to the widget, and it fails even when a valid indicator is supplied through the TabBarTheme. By moving the assertion into the method mentionned above, which runs at build time, the TabBar now has access to the TabBarTheme and can validate the indicator correctly, accepting indicatorWeight: 0 when a themed indicator is present, while still asserting when the default underline indicator is used. Fixes flutter/flutter#188837 ## Pre-Review Checklist
1 parent 6968b24 commit 149be41

3 files changed

Lines changed: 125 additions & 5 deletions

File tree

‎packages/material_ui/lib/src/tabs.dart‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
10691069
this.tabAlignment,
10701070
this.textScaler,
10711071
this.indicatorAnimation,
1072-
}) : _isPrimary = true,
1073-
assert(indicator != null || (indicatorWeight > 0.0));
1072+
}) : _isPrimary = true;
10741073

10751074
/// Creates a Material Design secondary tab bar.
10761075
///
@@ -1131,8 +1130,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
11311130
this.tabAlignment,
11321131
this.textScaler,
11331132
this.indicatorAnimation,
1134-
}) : _isPrimary = false,
1135-
assert(indicator != null || (indicatorWeight > 0.0));
1133+
}) : _isPrimary = false;
11361134

11371135
/// Typically a list of two or more [Tab] widgets.
11381136
///
@@ -1188,7 +1186,8 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
11881186
/// If [ThemeData.useMaterial3] is false, the default value is 2.0.
11891187
///
11901188
/// If [indicator] is specified or provided from [TabBarThemeData],
1191-
/// this property is ignored.
1189+
/// this property is ignored. Otherwise, when drawing the default underline
1190+
/// indicator, the value of this parameter must be greater than zero.
11921191
final double indicatorWeight;
11931192

11941193
/// The padding for the indicator.
@@ -1647,6 +1646,24 @@ class _TabBarState extends State<TabBar> {
16471646
if (tabBarTheme.indicator != null) {
16481647
return tabBarTheme.indicator!;
16491648
}
1649+
assert(() {
1650+
if (widget.indicatorWeight <= 0.0) {
1651+
throw FlutterError.fromParts(<DiagnosticsNode>[
1652+
ErrorSummary('Invalid indicatorWeight for TabBar.'),
1653+
ErrorDescription(
1654+
'The indicatorWeight must be greater than zero when the TabBar '
1655+
'draws its default underline indicator, that is when no indicator '
1656+
'is provided by the TabBar or the TabBarTheme.',
1657+
),
1658+
ErrorHint(
1659+
'To fix this, set indicatorWeight to a value greater than zero, '
1660+
'or provide an indicator with the TabBar.indicator or '
1661+
'TabBarThemeData.indicator property.',
1662+
),
1663+
]);
1664+
}
1665+
return true;
1666+
}());
16501667

16511668
Color color = widget.indicatorColor ?? tabBarTheme.indicatorColor ?? _defaults.indicatorColor!;
16521669
// ThemeData tries to avoid this by having indicatorColor avoid being the
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Allows `TabBar.indicatorWeight` of 0 when the indicator is supplied by `TabBarThemeData`.
3+
version: patch

‎packages/material_ui/test/tabs_test.dart‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter/gestures.dart';
77
import 'package:flutter/rendering.dart';
88
import 'package:flutter/services.dart';
99
import 'package:flutter_test/flutter_test.dart';
10+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1011
import 'package:material_ui/material_ui.dart';
1112

1213
import 'feedback_tester.dart';
@@ -3646,6 +3647,105 @@ void main() {
36463647
);
36473648
});
36483649

3650+
testWidgets('TabBar accepts indicatorWeight: 0 when the indicator comes from TabBarThemeData', (
3651+
WidgetTester tester,
3652+
) async {
3653+
// Regression test for https://github.com/flutter/flutter/issues/188837.
3654+
// A themed indicator supplies the decoration, so indicatorWeight: 0 must not
3655+
// throw the default-underline assertion.
3656+
const indicatorColor = Color(0xFF00FF00);
3657+
const Decoration indicator = BoxDecoration(color: indicatorColor);
3658+
const tabs = <Widget>[Tab(text: 'A'), Tab(text: 'B')];
3659+
3660+
Widget buildFrame({required bool secondary}) {
3661+
return boilerplate(
3662+
tabBarTheme: const TabBarThemeData(
3663+
indicator: indicator,
3664+
indicatorSize: TabBarIndicatorSize.tab,
3665+
),
3666+
child: Container(
3667+
alignment: Alignment.topLeft,
3668+
child: DefaultTabController(
3669+
length: tabs.length,
3670+
child: secondary
3671+
? const TabBar.secondary(indicatorWeight: 0.0, tabs: tabs)
3672+
: const TabBar(indicatorWeight: 0.0, tabs: tabs),
3673+
),
3674+
),
3675+
);
3676+
}
3677+
3678+
// Primary TabBar: no assertion is thrown and the themed indicator is painted.
3679+
await tester.pumpWidget(buildFrame(secondary: false));
3680+
expect(tester.takeException(), isNull);
3681+
3682+
final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
3683+
// 46 = _kTabHeight(46) with no weight added by the zero indicatorWeight.
3684+
expect(tabBarBox.size.height, 46.0);
3685+
expect(
3686+
tabBarBox,
3687+
paints..rect(rect: const Rect.fromLTRB(0.0, 0.0, 400.0, 46.0), color: indicatorColor),
3688+
);
3689+
3690+
// Secondary TabBar: also accepts a zero indicatorWeight with a themed indicator.
3691+
await tester.pumpWidget(buildFrame(secondary: true));
3692+
expect(tester.takeException(), isNull);
3693+
});
3694+
3695+
testWidgets('TabBar accepts indicatorWeight: 0 when the indicator is provided on the widget', (
3696+
WidgetTester tester,
3697+
) async {
3698+
const Decoration indicator = BoxDecoration(color: Color(0xFF00FF00));
3699+
const tabs = <Widget>[Tab(text: 'A'), Tab(text: 'B')];
3700+
3701+
await tester.pumpWidget(
3702+
boilerplate(
3703+
child: DefaultTabController(
3704+
length: tabs.length,
3705+
child: const TabBar(indicator: indicator, indicatorWeight: 0.0, tabs: tabs),
3706+
),
3707+
),
3708+
);
3709+
3710+
expect(tester.takeException(), isNull);
3711+
});
3712+
3713+
testWidgets(
3714+
'TabBar throws indicatorWeight: 0 with the default underline indicator',
3715+
experimentalLeakTesting: LeakTesting.settings
3716+
.withIgnoredAll(), // leaking by design because of exception
3717+
(WidgetTester tester) async {
3718+
// With no indicator on the widget or the theme, the TabBar falls back to the
3719+
// default underline indicator, which requires a positive indicatorWeight.
3720+
const tabs = <Widget>[Tab(text: 'A'), Tab(text: 'B')];
3721+
final Matcher throwsInvalidIndicatorWeightError = isFlutterError.having(
3722+
(FlutterError error) => error.message,
3723+
'message',
3724+
contains('Invalid indicatorWeight for TabBar.'),
3725+
);
3726+
3727+
await tester.pumpWidget(
3728+
boilerplate(
3729+
child: DefaultTabController(
3730+
length: tabs.length,
3731+
child: const TabBar(indicatorWeight: 0.0, tabs: tabs),
3732+
),
3733+
),
3734+
);
3735+
expect(tester.takeException(), throwsInvalidIndicatorWeightError);
3736+
3737+
await tester.pumpWidget(
3738+
boilerplate(
3739+
child: DefaultTabController(
3740+
length: tabs.length,
3741+
child: const TabBar.secondary(indicatorWeight: 0.0, tabs: tabs),
3742+
),
3743+
),
3744+
);
3745+
expect(tester.takeException(), throwsInvalidIndicatorWeightError);
3746+
},
3747+
);
3748+
36493749
testWidgets('TabBar with custom indicator - directional indicatorPadding (LTR)', (
36503750
WidgetTester tester,
36513751
) async {

0 commit comments

Comments
 (0)