From 673e9892302bd313b4c4332aa7d42ecc771c645a Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Wed, 27 Mar 2019 12:30:46 -0400 Subject: [PATCH] Remove unwanted gap between navigation bar and safe area's child (#29943) Remove the additional top padding from CupertinoPageScaffold's MediaQuery when the navigation bar is opaque (as the padding was already consumed by the navigation bar). Related Issue: #29136 --- .../lib/src/cupertino/page_scaffold.dart | 8 ++++-- .../flutter/test/cupertino/scaffold_test.dart | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/page_scaffold.dart b/packages/flutter/lib/src/cupertino/page_scaffold.dart index d9f0b074128..adc5a7a14cb 100644 --- a/packages/flutter/lib/src/cupertino/page_scaffold.dart +++ b/packages/flutter/lib/src/cupertino/page_scaffold.dart @@ -34,7 +34,8 @@ class CupertinoPageScaffold extends StatelessWidget { /// If translucent, the main content may slide behind it. /// Otherwise, the main content's top margin will be offset by its height. /// - /// The scaffold assumes the navigation bar will consume the [MediaQuery] top padding. + /// The scaffold assumes the navigation bar will account for the [MediaQuery] top padding, + /// also consume it if the navigation bar is opaque. // TODO(xster): document its page transition animation when ready final ObstructingPreferredSizeWidget navigationBar; @@ -92,7 +93,10 @@ class CupertinoPageScaffold extends StatelessWidget { // obstructed area. if (fullObstruction) { paddedContent = MediaQuery( - data: existingMediaQuery.copyWith( + data: existingMediaQuery + // If the navigation bar is opaque, the top media query padding is fully consumed by the navigation bar. + .removePadding(removeTop: true) + .copyWith( viewInsets: newViewInsets, ), child: Padding( diff --git a/packages/flutter/test/cupertino/scaffold_test.dart b/packages/flutter/test/cupertino/scaffold_test.dart index e42ec03fab7..34155715cda 100644 --- a/packages/flutter/test/cupertino/scaffold_test.dart +++ b/packages/flutter/test/cupertino/scaffold_test.dart @@ -25,6 +25,34 @@ void main() { expect(tester.getTopLeft(find.byType(Center)), const Offset(0.0, 0.0)); }); +testWidgets('Opaque bar pushes contents down', (WidgetTester tester) async { + BuildContext childContext; + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: const MediaQueryData(viewInsets: EdgeInsets.only(top: 20)), + child: CupertinoPageScaffold( + navigationBar: const CupertinoNavigationBar( + middle: Text('Opaque'), + backgroundColor: Color(0xFFF8F8F8), + ), + child: Builder( + builder: (BuildContext context) { + childContext = context; + return Container(); + }, + ), + ), + ), + )); + + expect(MediaQuery.of(childContext).padding.top, 0); + // The top of the [Container] is 44 px from the top of the screen because + // it's pushed down by the opaque navigation bar whose height is 44 px, + // and the 20 px [MediaQuery] top padding is fully absorbed by the navigation bar. + expect(tester.getRect(find.byType(Container)), Rect.fromLTRB(0, 44, 800, 600)); + }); + testWidgets('Contents padding from viewInsets', (WidgetTester tester) async { await tester.pumpWidget(Directionality( textDirection: TextDirection.ltr,