Skip to content

Commit bff417a

Browse files
authored
Update material banner maxScaleFactor to meet GAR requirement (#142015)
fixes: flutter/flutter#142012
1 parent e86c1c8 commit bff417a

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

packages/flutter/lib/src/material/banner.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'theme.dart';
1717

1818
const Duration _materialBannerTransitionDuration = Duration(milliseconds: 250);
1919
const Curve _materialBannerHeightCurve = Curves.fastOutSlowIn;
20+
const double _kMaxContentTextScaleFactor = 1.5;
2021

2122
/// Specify how a [MaterialBanner] was closed.
2223
///
@@ -365,21 +366,31 @@ class _MaterialBannerState extends State<MaterialBanner> {
365366
padding: leadingPadding,
366367
child: widget.leading,
367368
),
368-
Expanded(
369-
child: DefaultTextStyle(
370-
style: textStyle!,
371-
child: widget.content,
369+
MediaQuery.withClampedTextScaling(
370+
// Set maximum text scale factor to _kMaxContentTextScaleFactor for the
371+
// content to keep the visual hierarchy the same even with larger font
372+
// sizes.
373+
maxScaleFactor: _kMaxContentTextScaleFactor,
374+
child: Expanded(
375+
child: DefaultTextStyle(
376+
style: textStyle!,
377+
child: widget.content,
378+
),
372379
),
373380
),
374381
if (isSingleRow)
375-
actionsBar,
382+
MediaQuery.withClampedTextScaling(
383+
// Set maximum text scale factor to _kMaxContentTextScaleFactor for the
384+
// actionsBar to keep the visual hierarchy the same even with larger font
385+
// sizes.
386+
maxScaleFactor: _kMaxContentTextScaleFactor,
387+
child: actionsBar,
388+
),
376389
],
377390
),
378391
),
379-
if (!isSingleRow)
380-
actionsBar,
381-
if (elevation == 0)
382-
Divider(height: 0, color: dividerColor),
392+
if (!isSingleRow) actionsBar,
393+
if (elevation == 0) Divider(height: 0, color: dividerColor),
383394
],
384395
),
385396
),

packages/flutter/test/material/banner_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter/rendering.dart';
78
import 'package:flutter_test/flutter_test.dart';
@@ -236,6 +237,51 @@ void main() {
236237
expect(contentBottomLeft.dx, lessThan(actionsTopRight.dx));
237238
});
238239

240+
testWidgets('material banner content can scale and has maxScaleFactor', (WidgetTester tester) async {
241+
242+
const String label = 'A';
243+
Widget buildApp({ required TextScaler textScaler }) {
244+
return MaterialApp(
245+
home: MediaQuery(
246+
data: MediaQueryData(textScaler: textScaler),
247+
child: MaterialBanner(
248+
forceActionsBelow: true,
249+
content: const SizedBox(child:Center(child:Text(label))),
250+
actions: <Widget>[
251+
TextButton(
252+
child: const Text('B'),
253+
onPressed: () { },
254+
),
255+
],
256+
),
257+
),
258+
);
259+
}
260+
261+
await tester.pumpWidget(buildApp(textScaler: TextScaler.noScaling));
262+
expect(find.text(label), findsOneWidget);
263+
264+
if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
265+
expect(tester.getSize(find.text(label)), const Size(14.25, 20.0));
266+
}
267+
268+
await tester.pumpWidget(buildApp(textScaler: const TextScaler.linear(1.1)));
269+
await tester.pumpAndSettle();
270+
if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
271+
expect(_sizeAlmostEqual(tester.getSize(find.text(label)), const Size(15.65, 22.0)), true);
272+
}
273+
274+
await tester.pumpWidget(buildApp(textScaler: const TextScaler.linear(1.5)));
275+
if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
276+
expect(_sizeAlmostEqual(tester.getSize(find.text(label)), const Size(21.25, 30)), true);
277+
}
278+
279+
await tester.pumpWidget(buildApp(textScaler: const TextScaler.linear(4)));
280+
if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
281+
expect(_sizeAlmostEqual(tester.getSize(find.text(label)), const Size(21.25, 30)), true);
282+
}
283+
});
284+
239285
group('MaterialBanner elevation', () {
240286
Widget buildBanner(Key tapTarget, {double? elevation, double? themeElevation}) {
241287
return MaterialApp(
@@ -1117,3 +1163,7 @@ Material _getMaterialFromText(WidgetTester tester, String text) {
11171163
RenderParagraph _getTextRenderObjectFromDialog(WidgetTester tester, String text) {
11181164
return tester.element<StatelessElement>(find.descendant(of: find.byType(MaterialBanner), matching: find.text(text))).renderObject! as RenderParagraph;
11191165
}
1166+
1167+
bool _sizeAlmostEqual(Size a, Size b, {double maxDiff=0.05}) {
1168+
return (a.width - b.width).abs() <= maxDiff && (a.height - b.height).abs() <= maxDiff;
1169+
}

0 commit comments

Comments
 (0)