From 3a6c18daec59795fea534c4bd936250ae0410a31 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Mon, 27 Sep 2021 17:31:23 -0700 Subject: [PATCH] Correct notch geometry when MediaQuery padding.top is non-zero (#90703) --- .../lib/src/material/bottom_app_bar.dart | 7 +++ .../test/material/bottom_app_bar_test.dart | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/packages/flutter/lib/src/material/bottom_app_bar.dart b/packages/flutter/lib/src/material/bottom_app_bar.dart index 3ed65de192..fecb6c91bf 100644 --- a/packages/flutter/lib/src/material/bottom_app_bar.dart +++ b/packages/flutter/lib/src/material/bottom_app_bar.dart @@ -174,7 +174,14 @@ class _BottomAppBarClipper extends CustomClipper { final double notchMargin; // Returns the top of the BottomAppBar in global coordinates. + // + // If the Scaffold's bottomNavigationBar was specified, then we can use its + // geometry value, otherwise we compute the location based on the AppBar's + // Material widget. double get bottomNavigationBarTop { + final double? bottomNavigationBarTop = geometry.value.bottomNavigationBarTop; + if (bottomNavigationBarTop != null) + return bottomNavigationBarTop; final RenderBox? box = materialKey.currentContext?.findRenderObject() as RenderBox?; return box?.localToGlobal(Offset.zero).dy ?? 0; } diff --git a/packages/flutter/test/material/bottom_app_bar_test.dart b/packages/flutter/test/material/bottom_app_bar_test.dart index 00271fdab7..f252c01cbe 100644 --- a/packages/flutter/test/material/bottom_app_bar_test.dart +++ b/packages/flutter/test/material/bottom_app_bar_test.dart @@ -418,6 +418,67 @@ void main() { expect(tester.getRect(find.byType(FloatingActionButton)), const Rect.fromLTRB(372, 528, 428, 584)); expect(tester.getSize(find.byType(BottomAppBar)), const Size(800, 50)); }); + + testWidgets('notch with margin and top padding, home safe area', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/90024 + await tester.pumpWidget( + const MediaQuery( + data: MediaQueryData( + padding: EdgeInsets.only(top: 128), + ), + child: MaterialApp( + useInheritedMediaQuery: true, + home: SafeArea( + child: Scaffold( + bottomNavigationBar: ShapeListener( + BottomAppBar( + shape: RectangularNotch(), + notchMargin: 6.0, + child: SizedBox(height: 100.0), + ), + ), + floatingActionButton: FloatingActionButton( + onPressed: null, + child: Icon(Icons.add), + ), + floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, + ), + ), + ), + ), + ); + + final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener)); + final RenderBox babBox = tester.renderObject(find.byType(BottomAppBar)); + final Size babSize = babBox.size; + final RenderBox fabBox = tester.renderObject(find.byType(FloatingActionButton)); + final Size fabSize = fabBox.size; + + final double fabLeft = (babSize.width / 2.0) - (fabSize.width / 2.0) - 6.0; + final double fabRight = fabLeft + fabSize.width + 6.0; + final double fabBottom = 6.0 + fabSize.height / 2.0; + + final Path expectedPath = Path() + ..moveTo(0.0, 0.0) + ..lineTo(fabLeft, 0.0) + ..lineTo(fabLeft, fabBottom) + ..lineTo(fabRight, fabBottom) + ..lineTo(fabRight, 0.0) + ..lineTo(babSize.width, 0.0) + ..lineTo(babSize.width, babSize.height) + ..lineTo(0.0, babSize.height) + ..close(); + + final Path actualPath = shapeListenerState.cache.value; + + expect( + actualPath, + coversSameAreaAs( + expectedPath, + areaToCompare: (Offset.zero & babSize).inflate(5.0), + ), + ); + }); } // The bottom app bar clip path computation is only available at paint time.