Skip to content

Commit 1e622ce

Browse files
committed
Respect custom SnackBar.padding when action overflows
Only insert the trailing spacer SizedBox when the default padding is used, since a caller-supplied padding already applies its own end inset. Adds a unit test covering asymmetric SnackBar.padding with an overflowing action.
1 parent 88772d6 commit 1e622ce

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ class _SnackBarState extends State<SnackBar> {
794794
),
795795
),
796796
if (!willOverflowAction) ...maybeActionAndIcon,
797-
if (willOverflowAction) SizedBox(width: horizontalPadding),
797+
if (willOverflowAction && widget.padding == null) SizedBox(width: horizontalPadding),
798798
],
799799
),
800800
if (willOverflowAction)

‎packages/material_ui/test/snack_bar_test.dart‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4500,6 +4500,61 @@ void main() {
45004500
expect(contentRow.children.last, isA<SizedBox>());
45014501
expect((contentRow.children.last as SizedBox).width, 24.0);
45024502
});
4503+
4504+
testWidgets('SnackBar respects custom padding when action overflows', (
4505+
WidgetTester tester,
4506+
) async {
4507+
const double screenWidth = 500.0;
4508+
const EdgeInsets customPadding = EdgeInsets.only(left: 36, right: 48);
4509+
tester.view.physicalSize = const Size(screenWidth, 800.0);
4510+
tester.view.devicePixelRatio = 1.0;
4511+
addTearDown(tester.view.reset);
4512+
4513+
await tester.pumpWidget(
4514+
MaterialApp(
4515+
home: Scaffold(
4516+
body: Builder(
4517+
builder: (BuildContext context) {
4518+
return GestureDetector(
4519+
onTap: () {
4520+
ScaffoldMessenger.of(context).showSnackBar(
4521+
SnackBar(
4522+
content: const SizedBox(key: Key('content'), height: 20),
4523+
action: SnackBarAction(label: 'Overflow Action', onPressed: () {}),
4524+
actionOverflowThreshold: 0.1,
4525+
padding: customPadding,
4526+
),
4527+
);
4528+
},
4529+
child: const Text('Show'),
4530+
);
4531+
},
4532+
),
4533+
),
4534+
),
4535+
);
4536+
4537+
await tester.tap(find.text('Show'));
4538+
await tester.pumpAndSettle();
4539+
4540+
// When a custom padding is provided, it is applied on both sides by the outer
4541+
// Padding, so no extra spacer is inserted in the content row.
4542+
final Row contentRow = tester.widget<Row>(
4543+
find.descendant(of: find.byType(SnackBar), matching: find.byType(Row)).first,
4544+
);
4545+
expect(contentRow.children.length, 1);
4546+
expect(contentRow.children.single, isA<Expanded>());
4547+
4548+
expect(
4549+
tester.getSize(find.byKey(const Key('content'))).width,
4550+
screenWidth - customPadding.horizontal,
4551+
);
4552+
expect(tester.getTopLeft(find.byKey(const Key('content'))).dx, customPadding.left);
4553+
expect(
4554+
tester.getTopRight(find.byKey(const Key('content'))).dx,
4555+
screenWidth - customPadding.right,
4556+
);
4557+
});
45034558
}
45044559

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

0 commit comments

Comments
 (0)