Skip to content

Commit 2a9ec69

Browse files
[material_ui] Fix minimum width for Material 3 primary tab indicator (#13322)
This change gives the default Material 3 primary `TabBar` indicator a minimum width of 24dp, centered on the selected label, as specified by the [Material 3 tabs spec](https://m3.material.io/components/tabs/specs). It adds a regression test for a short tab label. Custom indicators and secondary tabs retain their existing sizing behavior. part of flutter/flutter#194716 ## Testing - `dart run script/tool/bin/flutter_plugin_tools.dart test --packages=material_ui` — passed for all four targets: `material_ui`, its example, the accessibility assessments app, and `gen_defaults`. - `flutter test test/tabs_test.dart` — passed (204 tests), including the 24dp regression test. - `flutter analyze lib/src/tabs.dart test/tabs_test.dart` — no issues found. - `dart format lib/src/tabs.dart test/tabs_test.dart` — completed; no formatting changes were needed. ## Pre-Review Checklist
1 parent 62099ac commit 2a9ec69

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ part 'generated/tabs_defaults_m3.g.dart';
3232
const double _kTabHeight = 46.0;
3333
const double _kTextAndIconTabHeight = 72.0;
3434
const double _kStartOffset = 52.0;
35+
const double _kMinPrimaryTabIndicatorWidth = 24.0;
3536

3637
/// Defines how the bounds of the selected tab indicator are computed.
3738
///
@@ -492,6 +493,7 @@ class _IndicatorPainter extends CustomPainter {
492493
required TabController controller,
493494
required Decoration indicator,
494495
required TabBarIndicatorSize indicatorSize,
496+
required double? minimumIndicatorWidth,
495497
required List<GlobalKey> tabKeys,
496498
required _IndicatorPainter? old,
497499
required EdgeInsetsGeometry indicatorPadding,
@@ -513,6 +515,7 @@ class _IndicatorPainter extends CustomPainter {
513515
controller: controller,
514516
indicator: indicator,
515517
indicatorSize: indicatorSize,
518+
minimumIndicatorWidth: minimumIndicatorWidth,
516519
tabKeys: tabKeys,
517520
old: old,
518521
indicatorPadding: indicatorPadding,
@@ -531,6 +534,7 @@ class _IndicatorPainter extends CustomPainter {
531534
required this.controller,
532535
required this.indicator,
533536
required this.indicatorSize,
537+
required this.minimumIndicatorWidth,
534538
required this.tabKeys,
535539
required _IndicatorPainter? old,
536540
required this.indicatorPadding,
@@ -553,6 +557,7 @@ class _IndicatorPainter extends CustomPainter {
553557
final TabController controller;
554558
final Decoration indicator;
555559
final TabBarIndicatorSize indicatorSize;
560+
final double? minimumIndicatorWidth;
556561
final EdgeInsetsGeometry indicatorPadding;
557562
final List<GlobalKey> tabKeys;
558563
final List<EdgeInsetsGeometry> labelPaddings;
@@ -622,6 +627,14 @@ class _IndicatorPainter extends CustomPainter {
622627
tabRight = tabLeft + tabWidth;
623628
}
624629

630+
final double indicatorWidth = tabRight - tabLeft;
631+
final double? minimumWidth = minimumIndicatorWidth;
632+
if (minimumWidth != null && indicatorWidth < minimumWidth) {
633+
final double tabCenter = (tabLeft + tabRight) / 2.0;
634+
tabLeft = tabCenter - minimumWidth / 2.0;
635+
tabRight = tabCenter + minimumWidth / 2.0;
636+
}
637+
625638
final EdgeInsets insets = indicatorPadding.resolve(_currentTextDirection);
626639
final rect = Rect.fromLTWH(tabLeft, 0.0, tabRight - tabLeft, tabBarSize.height);
627640

@@ -772,6 +785,7 @@ class _IndicatorPainter extends CustomPainter {
772785
return _needsPaint ||
773786
controller != old.controller ||
774787
indicator != old.indicator ||
788+
minimumIndicatorWidth != old.minimumIndicatorWidth ||
775789
tabKeys.length != old.tabKeys.length ||
776790
(!listEquals(_currentTabOffsets, old._currentTabOffsets)) ||
777791
_currentTextDirection != old._currentTextDirection;
@@ -1771,6 +1785,12 @@ class _TabBarState extends State<TabBar> {
17711785
final TabBarThemeData tabBarTheme = TabBarTheme.of(context);
17721786
final TabBarIndicatorSize indicatorSize =
17731787
widget.indicatorSize ?? tabBarTheme.indicatorSize ?? _defaults.indicatorSize!;
1788+
final bool usesDefaultPrimaryLabelIndicator =
1789+
theme.useMaterial3 &&
1790+
widget._isPrimary &&
1791+
indicatorSize == TabBarIndicatorSize.label &&
1792+
widget.indicator == null &&
1793+
tabBarTheme.indicator == null;
17741794

17751795
final _IndicatorPainter? oldPainter = _indicatorPainter;
17761796

@@ -1785,6 +1805,9 @@ class _TabBarState extends State<TabBar> {
17851805
controller: _controller!,
17861806
indicator: _getIndicator(indicatorSize),
17871807
indicatorSize: indicatorSize,
1808+
minimumIndicatorWidth: usesDefaultPrimaryLabelIndicator
1809+
? _kMinPrimaryTabIndicatorWidth
1810+
: null,
17881811
indicatorPadding: widget.indicatorPadding,
17891812
tabKeys: _tabKeys,
17901813
// Passing old painter so that the constructor can copy some values from it.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Makes the default Material 3 primary TabBar indicator at least 24dp wide. Use a custom indicator to keep the previous width.
3+
version: patch

‎packages/material_ui/test/tabs_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,39 @@ void main() {
511511
);
512512
});
513513

514+
testWidgets('Material 3 primary tab indicator has a minimum width of 24dp', (
515+
WidgetTester tester,
516+
) async {
517+
const tabs = <String>['A', 'Medium', 'Longer Tab'];
518+
await tester.pumpWidget(buildFrame(tabs: tabs, value: 'A', useMaterial3: true));
519+
520+
final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
521+
final RenderBox labelBox = tester.renderObject<RenderBox>(find.text('A'));
522+
expect(labelBox.size.width, lessThan(24.0));
523+
524+
final Offset labelCenter = tabBarBox.globalToLocal(
525+
labelBox.localToGlobal(labelBox.size.center(Offset.zero)),
526+
);
527+
final expectedIndicator = Rect.fromLTRB(
528+
labelCenter.dx - 12.0,
529+
tabBarBox.size.height - 3.0,
530+
labelCenter.dx + 12.0,
531+
tabBarBox.size.height,
532+
);
533+
534+
expect(
535+
tabBarBox,
536+
paints..rrect(
537+
color: Theme.of(tester.element(find.byType(TabBar))).colorScheme.primary,
538+
rrect: RRect.fromRectAndCorners(
539+
expectedIndicator,
540+
topLeft: const Radius.circular(3.0),
541+
topRight: const Radius.circular(3.0),
542+
),
543+
),
544+
);
545+
});
546+
514547
testWidgets('TabBar default tab indicator (secondary)', (WidgetTester tester) async {
515548
final theme = ThemeData();
516549
final tabs = List<Widget>.generate(4, (int index) {

0 commit comments

Comments
 (0)