Skip to content

Commit

Permalink
Update navigationBar label's maxScaleFactor to meet GAR requirement (…
Browse files Browse the repository at this point in the history
…#141998)

fixes: flutter/flutter#141997
  • Loading branch information
hannah-hyj authored Jan 24, 2024
1 parent 8fb4a64 commit 6adc824
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/flutter/lib/src/material/navigation_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'tooltip.dart';

const double _kIndicatorHeight = 32;
const double _kIndicatorWidth = 64;
const double _kMaxLabelTextScaleFactor = 1.3;

// Examples can assume:
// late BuildContext context;
Expand Down Expand Up @@ -422,9 +423,11 @@ class NavigationDestination extends StatelessWidget {
return Padding(
padding: const EdgeInsets.only(top: 4),
child: MediaQuery.withClampedTextScaling(
// Don't scale labels of destinations, instead, tooltip text will
// upscale.
maxScaleFactor: 1.0,
// Set maximum text scale factor to _kMaxLabelTextScaleFactor for the
// label to keep the visual hierarchy the same even with larger font
// sizes. To opt out, wrap the [label] widget in a [MediaQuery] widget
// with a different `TextScaler`.
maxScaleFactor: _kMaxLabelTextScaleFactor,
child: Text(label, style: textStyle),
),
);
Expand Down
70 changes: 70 additions & 0 deletions packages/flutter/test/material/navigation_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,71 @@ void main() {
}
});

testWidgets('Material3 - NavigationBar label can scale and has maxScaleFactor', (WidgetTester tester) async {
const String label = 'A';

Widget buildApp({ required TextScaler textScaler }) {
return MediaQuery(
data: MediaQueryData(textScaler: textScaler),
child: Localizations(
locale: const Locale('en', 'US'),
delegates: const <LocalizationsDelegate<dynamic>>[
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
],
child: MaterialApp(
home: Navigator(
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute<void>(
builder: (BuildContext context) {
return Scaffold(
bottomNavigationBar: NavigationBar(
destinations: const <NavigationDestination>[
NavigationDestination(
label: label,
icon: Icon(Icons.ac_unit),
),
NavigationDestination(
label: 'B',
icon: Icon(Icons.battery_alert),
),
],
),
);
},
);
},
),
),
),
);
}

await tester.pumpWidget(buildApp(textScaler: TextScaler.noScaling));
expect(find.text(label), findsOneWidget);
if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
expect( _sizeAlmostEqual(tester.getSize(find.text(label)), const Size(12.5, 16.0)), true);
}

await tester.pumpWidget(buildApp(textScaler: const TextScaler.linear(1.1)));
await tester.pumpAndSettle();

if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
expect( _sizeAlmostEqual(tester.getSize(find.text(label)), const Size(13.7, 18.0)), true);
}

await tester.pumpWidget(buildApp(textScaler: const TextScaler.linear(1.3)));

if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
expect( _sizeAlmostEqual(tester.getSize(find.text(label)), const Size(16.1, 21.0)), true);
}

await tester.pumpWidget(buildApp(textScaler: const TextScaler.linear(4)));
if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
expect( _sizeAlmostEqual(tester.getSize(find.text(label)), const Size(16.1, 21.0)), true);
}
});

testWidgets('Custom tooltips in NavigationBarDestination', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
Expand Down Expand Up @@ -1499,3 +1564,8 @@ class IconWithRandomColor extends StatelessWidget {
return Icon(icon, color: randomColor);
}
}


bool _sizeAlmostEqual(Size a, Size b, {double maxDiff=0.05}) {
return (a.width - b.width).abs() <= maxDiff && (a.height - b.height).abs() <= maxDiff;
}

0 comments on commit 6adc824

Please sign in to comment.