Skip to content

Commit b3d80fe

Browse files
[material_ui] Ensure textScaler is taken into account for SnackBar action overflow (flutter#12737)
Port of flutter#181959 to `flutter/packages` as part of the Material decoupling effort (flutter#188444). Ensure textScalling is taken into account for Sizing overflow. Fixes flutter#192232 Fixes flutter#181959 Part of flutter#188444 ### Description - Ensure textScalling is taken into account for Sizing overflow: passes `MediaQuery.textScalerOf(context)` to `actionTextPainter` so that `actionAndIconWidth` properly scales with accessibility text scaling. - Removes `SizedBox(width: snackBarWidth * 0.4)` when the action overflows, eliminating unintended blank space on the right of multi-line content. - Adds unit tests in `snack_bar_test.dart` verifying `textScaler` overflow calculation and layout. - Adds pending changelog entry. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 3744219 commit b3d80fe

3 files changed

Lines changed: 163 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ class _SnackBarState extends State<SnackBar> {
738738
),
739739
maxLines: 1,
740740
textDirection: TextDirection.ltr,
741+
textScaler: MediaQuery.textScalerOf(context),
741742
)..layout();
742743
final double actionAndIconWidth =
743744
actionTextPainter.size.width +
@@ -795,7 +796,7 @@ class _SnackBarState extends State<SnackBar> {
795796
),
796797
),
797798
if (!willOverflowAction) ...maybeActionAndIcon,
798-
if (willOverflowAction) SizedBox(width: snackBarWidth * 0.4),
799+
if (willOverflowAction && widget.padding == null) SizedBox(width: horizontalPadding),
799800
],
800801
),
801802
if (willOverflowAction)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Accounts for `MediaQuery`'s `TextScaler` when calculating `SnackBar` action overflow and removes unnecessary blank right spacing in multi-line layout.
3+
version: patch

‎packages/material_ui/test/snack_bar_test.dart‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,6 +4385,164 @@ void main() {
43854385
);
43864386
expect(tester.getSize(find.byType(SnackBarAction)), Size.zero);
43874387
});
4388+
4389+
testWidgets('SnackBar action overflow calculation respects MediaQuery textScaler', (
4390+
WidgetTester tester,
4391+
) async {
4392+
Widget buildSnackBar({required TextScaler textScaler}) {
4393+
return MaterialApp(
4394+
home: MediaQuery(
4395+
data: MediaQueryData(size: const Size(800, 600), textScaler: textScaler),
4396+
child: Scaffold(
4397+
body: Builder(
4398+
builder: (BuildContext context) {
4399+
return GestureDetector(
4400+
onTap: () {
4401+
ScaffoldMessenger.of(context).showSnackBar(
4402+
SnackBar(
4403+
content: const Text('Message'),
4404+
action: SnackBarAction(label: 'Action Label', onPressed: () {}),
4405+
),
4406+
);
4407+
},
4408+
child: const Text('Show'),
4409+
);
4410+
},
4411+
),
4412+
),
4413+
),
4414+
);
4415+
}
4416+
4417+
// With no scaling, the action label fits on the same row as content.
4418+
await tester.pumpWidget(buildSnackBar(textScaler: TextScaler.noScaling));
4419+
await tester.tap(find.text('Show'));
4420+
await tester.pumpAndSettle();
4421+
4422+
final Offset actionTopLeft1 = tester.getTopLeft(find.text('Action Label'));
4423+
// Action and content are on the same line (action is not below content).
4424+
expect(actionTopLeft1.dy, lessThanOrEqualTo(tester.getBottomLeft(find.text('Message')).dy));
4425+
4426+
ScaffoldMessenger.of(tester.element(find.text('Show'))).clearSnackBars();
4427+
await tester.pumpAndSettle();
4428+
4429+
// With a large text scaler, the action button width is scaled up and overflows
4430+
// to a separate row below the content.
4431+
await tester.pumpWidget(buildSnackBar(textScaler: const TextScaler.linear(3.0)));
4432+
await tester.tap(find.text('Show'));
4433+
await tester.pumpAndSettle();
4434+
4435+
final Offset contentBottomLeft2 = tester.getBottomLeft(find.text('Message'));
4436+
final Offset actionTopLeft2 = tester.getTopLeft(find.text('Action Label'));
4437+
// Action overflows and is positioned below the content text.
4438+
expect(actionTopLeft2.dy, greaterThanOrEqualTo(contentBottomLeft2.dy));
4439+
});
4440+
4441+
testWidgets('SnackBar does not allocate 40% empty space on right when action overflows', (
4442+
WidgetTester tester,
4443+
) async {
4444+
const screenWidth = 500.0;
4445+
tester.view.physicalSize = const Size(screenWidth, 800.0);
4446+
tester.view.devicePixelRatio = 1.0;
4447+
addTearDown(tester.view.reset);
4448+
4449+
await tester.pumpWidget(
4450+
MaterialApp(
4451+
home: Scaffold(
4452+
body: Builder(
4453+
builder: (BuildContext context) {
4454+
return GestureDetector(
4455+
onTap: () {
4456+
ScaffoldMessenger.of(context).showSnackBar(
4457+
SnackBar(
4458+
content: const SizedBox(key: Key('content'), height: 20),
4459+
action: SnackBarAction(label: 'Overflow Action', onPressed: () {}),
4460+
actionOverflowThreshold: 0.1,
4461+
),
4462+
);
4463+
},
4464+
child: const Text('Show'),
4465+
);
4466+
},
4467+
),
4468+
),
4469+
),
4470+
);
4471+
4472+
await tester.tap(find.text('Show'));
4473+
await tester.pumpAndSettle();
4474+
4475+
// In unfixed code, when the action overflowed to a second row, a SizedBox(width: snackBarWidth * 0.4)
4476+
// was placed next to the content Expanded widget in the first row, reserving 40% empty space on the right (188px).
4477+
// The content is now given standard horizontalPadding on the right (24px) instead of 40% empty space.
4478+
// For screenWidth = 500, horizontalPadding = 24 on start and end, the content width is 452px (500 - 24 - 24).
4479+
expect(tester.getSize(find.byKey(const Key('content'))).width, 452.0);
4480+
expect(tester.getTopRight(find.byKey(const Key('content'))).dx, screenWidth - 24.0);
4481+
4482+
// Verify the spacer SizedBox has width equal to horizontalPadding (24.0), not 40% of snackBarWidth (188.0).
4483+
final Row contentRow = tester.widget<Row>(
4484+
find.descendant(of: find.byType(SnackBar), matching: find.byType(Row)).first,
4485+
);
4486+
expect(contentRow.children.length, 2);
4487+
expect(contentRow.children.first, isA<Expanded>());
4488+
expect(contentRow.children.last, isA<SizedBox>());
4489+
expect((contentRow.children.last as SizedBox).width, 24.0);
4490+
});
4491+
4492+
testWidgets('SnackBar respects custom padding when action overflows', (
4493+
WidgetTester tester,
4494+
) async {
4495+
const screenWidth = 500.0;
4496+
const customPadding = EdgeInsets.only(left: 36, right: 48);
4497+
tester.view.physicalSize = const Size(screenWidth, 800.0);
4498+
tester.view.devicePixelRatio = 1.0;
4499+
addTearDown(tester.view.reset);
4500+
4501+
await tester.pumpWidget(
4502+
MaterialApp(
4503+
home: Scaffold(
4504+
body: Builder(
4505+
builder: (BuildContext context) {
4506+
return GestureDetector(
4507+
onTap: () {
4508+
ScaffoldMessenger.of(context).showSnackBar(
4509+
SnackBar(
4510+
content: const SizedBox(key: Key('content'), height: 20),
4511+
action: SnackBarAction(label: 'Overflow Action', onPressed: () {}),
4512+
actionOverflowThreshold: 0.1,
4513+
padding: customPadding,
4514+
),
4515+
);
4516+
},
4517+
child: const Text('Show'),
4518+
);
4519+
},
4520+
),
4521+
),
4522+
),
4523+
);
4524+
4525+
await tester.tap(find.text('Show'));
4526+
await tester.pumpAndSettle();
4527+
4528+
// When a custom padding is provided, it is applied on both sides by the outer
4529+
// Padding, so no extra spacer is inserted in the content row.
4530+
final Row contentRow = tester.widget<Row>(
4531+
find.descendant(of: find.byType(SnackBar), matching: find.byType(Row)).first,
4532+
);
4533+
expect(contentRow.children.length, 1);
4534+
expect(contentRow.children.single, isA<Expanded>());
4535+
4536+
expect(
4537+
tester.getSize(find.byKey(const Key('content'))).width,
4538+
screenWidth - customPadding.horizontal,
4539+
);
4540+
expect(tester.getTopLeft(find.byKey(const Key('content'))).dx, customPadding.left);
4541+
expect(
4542+
tester.getTopRight(find.byKey(const Key('content'))).dx,
4543+
screenWidth - customPadding.right,
4544+
);
4545+
});
43884546
}
43894547

43904548
/// Start test for "SnackBar dismiss test".

0 commit comments

Comments
 (0)