diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index eaf757f5fb..17b87fa6a6 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -578,7 +578,15 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { if (floatingActionButtonRect.size != Size.zero && isSnackBarFloating) { snackBarYOffsetBase = floatingActionButtonRect.top; } else { - snackBarYOffsetBase = contentBottom; + // SnackBarBehavior.fixed applies a SafeArea automatically. + // SnackBarBehavior.floating does not since the positioning is affected + // if there is a FloatingActionButton (see condition above). If there is + // no FAB, make sure we account for safe space when the SnackBar is + // floating. + final double safeYOffsetBase = size.height - minViewPadding.bottom; + snackBarYOffsetBase = isSnackBarFloating + ? math.min(contentBottom, safeYOffsetBase) + : contentBottom; } final double xOffset = hasCustomWidth ? (size.width - snackBarWidth) / 2 : 0.0; diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 9946603357..6e28582ed3 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -420,6 +420,53 @@ void main() { expect(snackBarBottomRight.dx, 800 - padding); // Device width is 800. }); + testWidgets('SnackbarBehavior.floating is positioned within safe area', (WidgetTester tester) async { + const double viewPadding = 50.0; + const double floatingSnackBarDefaultBottomMargin = 10.0; + await tester.pumpWidget( + MaterialApp( + home: MediaQuery( + data: const MediaQueryData( + // Simulate non-safe area. + viewPadding: EdgeInsets.only(bottom: viewPadding), + ), + child: Scaffold( + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + onTap: () { + Scaffold.of(context).showSnackBar( + const SnackBar( + content: Text('I am a snack bar.'), + behavior: SnackBarBehavior.floating, + ), + ); + }, + child: const Text('X'), + ); + } + ), + ), + ), + ), + ); + + await tester.tap(find.text('X')); + await tester.pump(); // Start animation + await tester.pump(const Duration(milliseconds: 750)); + + final Finder materialFinder = find.descendant( + of: find.byType(SnackBar), + matching: find.byType(Material), + ); + final Offset snackBarBottomLeft = tester.getBottomLeft(materialFinder); + expect( + snackBarBottomLeft.dy, + // Device height is 600. + 600 - viewPadding - floatingSnackBarDefaultBottomMargin, + ); + }); + testWidgets('Snackbar padding can be customized', (WidgetTester tester) async { const double padding = 20.0; await tester.pumpWidget(