diff --git a/packages/flutter/lib/src/widgets/safe_area.dart b/packages/flutter/lib/src/widgets/safe_area.dart index cf74e01c88..065262b9f4 100644 --- a/packages/flutter/lib/src/widgets/safe_area.dart +++ b/packages/flutter/lib/src/widgets/safe_area.dart @@ -71,10 +71,9 @@ class SafeArea extends StatelessWidget { /// The greater of the minimum insets and the media padding will be applied. final EdgeInsets minimum; - /// Specifies whether the [SafeArea] should maintain the - /// [MediaQueryData.viewPadding] instead of the [MediaQueryData.padding] when - /// consumed by the [MediaQueryData.viewInsets] of the current context's - /// [MediaQuery], defaults to false. + /// Specifies whether the [SafeArea] should maintain the bottom + /// [MediaQueryData.viewPadding] instead of the bottom [MediaQueryData.padding], + /// defaults to false. /// /// For example, if there is an onscreen keyboard displayed above the /// SafeArea, the padding can be maintained below the obstruction rather than @@ -98,7 +97,7 @@ class SafeArea extends StatelessWidget { final MediaQueryData data = MediaQuery.of(context); EdgeInsets padding = data.padding; // Bottom padding has been consumed - i.e. by the keyboard - if (data.padding.bottom == 0.0 && data.viewInsets.bottom != 0.0 && maintainBottomViewPadding) + if (maintainBottomViewPadding) padding = padding.copyWith(bottom: data.viewPadding.bottom); return Padding( diff --git a/packages/flutter/test/widgets/safe_area_test.dart b/packages/flutter/test/widgets/safe_area_test.dart index 2b3494648b..92c5fb6803 100644 --- a/packages/flutter/test/widgets/safe_area_test.dart +++ b/packages/flutter/test/widgets/safe_area_test.dart @@ -147,6 +147,39 @@ void main() { expect(initialPoint, finalPoint); }); + testWidgets('SafeArea alone - partial ViewInsets consume Padding', (WidgetTester tester) async { + final Widget child = boilerplate(SafeArea( + maintainBottomViewPadding: true, + child: Column( + children: const [ + Expanded(child: Placeholder()), + ], + ), + )); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewPadding: EdgeInsets.only(bottom: 20.0), + ), + child: child, + ), + ); + final Offset initialPoint = tester.getCenter(find.byType(Placeholder)); + // Consume bottom padding - as if by the keyboard opening + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewPadding: EdgeInsets.only(bottom: 20.0), + viewInsets: EdgeInsets.only(bottom: 10.0), + ), + child: child, + ), + ); + final Offset finalPoint = tester.getCenter(find.byType(Placeholder)); + expect(initialPoint, finalPoint); + }); + testWidgets('SafeArea with nested Scaffold', (WidgetTester tester) async { final Widget child = boilerplate(SafeArea( maintainBottomViewPadding: true, @@ -184,6 +217,42 @@ void main() { final Offset finalPoint = tester.getCenter(find.byType(Placeholder)); expect(initialPoint, finalPoint); }); + + testWidgets('SafeArea with nested Scaffold - partial ViewInsets consume Padding', (WidgetTester tester) async { + final Widget child = boilerplate(SafeArea( + maintainBottomViewPadding: true, + child: Scaffold( + resizeToAvoidBottomInset: false, + body: Column( + children: const [ + Expanded(child: Placeholder()), + ], + ), + ), + )); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewPadding: EdgeInsets.only(bottom: 20.0), + ), + child: child, + ), + ); + final Offset initialPoint = tester.getCenter(find.byType(Placeholder)); + // Consume bottom padding - as if by the keyboard opening + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewPadding: EdgeInsets.only(bottom: 20.0), + viewInsets: EdgeInsets.only(bottom: 10.0), + ), + child: child, + ), + ); + final Offset finalPoint = tester.getCenter(find.byType(Placeholder)); + expect(initialPoint, finalPoint); + }); }); });