From 531870f57b8c26a3f6bc35e3fcbc9cdec3f698ad Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 9 Feb 2021 17:41:03 -0800 Subject: [PATCH] Fix constraints of popupmenu (#75748) --- .../flutter/lib/src/material/popup_menu.dart | 4 +- .../test/material/popup_menu_test.dart | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 1d3ea6b101..f65e68240e 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -623,9 +623,7 @@ class _PopupMenuRouteLayout extends SingleChildLayoutDelegate { BoxConstraints getConstraintsForChild(BoxConstraints constraints) { // The menu can be at most the size of the overlay minus 8.0 pixels in each // direction. - return BoxConstraints.loose( - constraints.biggest - const Offset(_kMenuScreenPadding * 2.0, _kMenuScreenPadding * 2.0) as Size, - ); + return BoxConstraints.loose(constraints.biggest).deflate(const EdgeInsets.all(_kMenuScreenPadding)); } @override diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 713907474f..e2b0c37046 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -1915,6 +1915,46 @@ void main() { await buildFrame(iconSize: 50); expect(tester.widget(find.byType(IconButton)).iconSize, 50); }); + + testWidgets('does not crash in small overlay', (WidgetTester tester) async { + final GlobalKey navigator = GlobalKey(); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Column( + children: [ + OutlinedButton( + onPressed: () { + showMenu( + context: navigator.currentContext!, + position: const RelativeRect.fromLTRB(0, 0, 0, 0), + items: const >[ + PopupMenuItem(child: Text('foo')), + ], + ); + }, + child: const Text('press'), + ), + SizedBox( + height: 10, + width: 10, + child: Navigator( + key: navigator, + onGenerateRoute: (RouteSettings settings) => MaterialPageRoute( + builder: (BuildContext context) => Container(color: Colors.red), + ), + ), + ), + ], + ), + ), + ), + ); + + await tester.tap(find.text('press')); + await tester.pumpAndSettle(); + expect(find.text('foo'), findsOneWidget); + }); } class TestApp extends StatefulWidget {