diff --git a/packages/flutter/lib/src/cupertino/context_menu.dart b/packages/flutter/lib/src/cupertino/context_menu.dart index d8a5da91cd..3311ad8d3d 100644 --- a/packages/flutter/lib/src/cupertino/context_menu.dart +++ b/packages/flutter/lib/src/cupertino/context_menu.dart @@ -21,9 +21,14 @@ import 'localizations.dart'; // This value was eyeballed from a physical device running iOS 13.1.2. const double _kOpenScale = 1.15; +// The smallest possible scale of the child, used if opening the +// CupertinoContextMenu would cause it to go outside the safe area. This value +// was eyeballed from the Xcode iPhone simulator running iOS 16.1. +const double _kMinScaleFactor = 1.02; + // The ratio for the borderRadius of the context menu preview image. This value // was eyeballed by overlapping the CupertinoContextMenu with a context menu -// from iOS 16.0 in the XCode iPhone simulator. +// from iOS 16.0 in the Xcode iPhone simulator. const double _previewBorderRadiusRatio = 12.0; // The duration of the transition used when a modal popup is shown. Eyeballed @@ -31,7 +36,7 @@ const double _previewBorderRadiusRatio = 12.0; const Duration _kModalPopupTransitionDuration = Duration(milliseconds: 335); // The duration it takes for the CupertinoContextMenu to open. -// This value was eyeballed from the XCode simulator running iOS 16.0. +// This value was eyeballed from the Xcode simulator running iOS 16.0. const Duration _previewLongPressTimeout = Duration(milliseconds: 800); // The total length of the combined animations until the menu is fully open. @@ -39,7 +44,7 @@ final int _animationDuration = _previewLongPressTimeout.inMilliseconds + _kModalPopupTransitionDuration.inMilliseconds; // The final box shadow for the opening child widget. -// This value was eyeballed from the XCode simulator running iOS 16.0. +// This value was eyeballed from the Xcode simulator running iOS 16.0. const List _endBoxShadow = [ BoxShadow( color: Color(0x40000000), @@ -369,6 +374,7 @@ class _CupertinoContextMenuState extends State with Ticker // Animates the child while it's opening. late AnimationController _openController; Rect? _decoyChildEndRect; + late double _scaleFactor; OverlayEntry? _lastOverlayEntry; _ContextMenuRoute? _route; final double _midpoint = CupertinoContextMenu.animationOpensAt / 2; @@ -428,9 +434,22 @@ class _CupertinoContextMenuState extends State with Ticker return _ContextMenuLocation.left; } + // Constrain the size of the expanded child so that it does not go outside the + // safe area. See https://github.com/flutter/flutter/issues/122951. + static double _computeScaleFactor(Rect childRect, EdgeInsets padding, Size size) { + final double leftMaxScale = 2 * (childRect.center.dx - padding.left) / childRect.width; + final double topMaxScale = 2 * (childRect.center.dy - padding.top) / childRect.height; + final double rightMaxScale = 2 * (size.width - padding.right - childRect.center.dx) / childRect.width; + final double bottomMaxScale = 2 * (size.height - padding.bottom - childRect.center.dy) / childRect.height; + final double minWidth = math.min(leftMaxScale, rightMaxScale); + final double minHeight = math.min(topMaxScale, bottomMaxScale); + // Return the smallest scale factor that keeps the child mostly onscreen. + return clampDouble(math.min(minWidth, minHeight), _kMinScaleFactor, _kOpenScale); + } + /// The default preview builder if none is provided. It makes a rectangle /// around the child widget with rounded borders, matching the iOS 16 opened - /// context menu eyeballed on the XCode iOS simulator. + /// context menu eyeballed on the Xcode iOS simulator. static Widget _defaultPreviewBuilder(BuildContext context, Animation animation, Widget child) { return FittedBox( fit: BoxFit.cover, @@ -456,6 +475,7 @@ class _CupertinoContextMenuState extends State with Ticker ), contextMenuLocation: _contextMenuLocation, previousChildRect: _decoyChildEndRect!, + scaleFactor: _scaleFactor, builder: (BuildContext context, Animation animation) { if (widget.child == null) { final Animation localAnimation = Tween(begin: CupertinoContextMenu.animationOpensAt, end: 1).animate(animation); @@ -545,10 +565,13 @@ class _CupertinoContextMenuState extends State with Ticker }); final Rect childRect = _getRect(_childGlobalKey); + final EdgeInsets safeAreaPadding = MediaQuery.paddingOf(context); + final Size screenSize = MediaQuery.sizeOf(context); + _scaleFactor = _computeScaleFactor(childRect, safeAreaPadding, screenSize); _decoyChildEndRect = Rect.fromCenter( center: childRect.center, - width: childRect.width * _kOpenScale, - height: childRect.height * _kOpenScale, + width: childRect.width * _scaleFactor, + height: childRect.height * _scaleFactor, ); // Create a decoy child in an overlay directly on top of the original child. @@ -641,7 +664,7 @@ class _DecoyChildState extends State<_DecoyChild> with TickerProviderStateMixin final double endPause = ((totalOpenAnimationLength * _animationDuration) / _previewLongPressTimeout.inMilliseconds) - totalOpenAnimationLength; - // The timing on the animation was eyeballed from the XCode iOS simulator + // The timing on the animation was eyeballed from the Xcode iOS simulator // running iOS 16.0. // Because the animation no longer goes from 0.0 to 1.0, but to a number // depending on the ratio between the press animation time and the opening @@ -734,12 +757,14 @@ class _ContextMenuRoute extends PopupRoute { CupertinoContextMenuBuilder? builder, super.filter, required Rect previousChildRect, + required double scaleFactor, super.settings, }) : assert(actions.isNotEmpty), _actions = actions, _builder = builder, _contextMenuLocation = contextMenuLocation, - _previousChildRect = previousChildRect; + _previousChildRect = previousChildRect, + _scaleFactor = scaleFactor; // Barrier color for a Cupertino modal barrier. static const Color _kModalBarrierColor = Color(0x6604040F); @@ -750,6 +775,7 @@ class _ContextMenuRoute extends PopupRoute { final _ContextMenuLocation _contextMenuLocation; bool _externalOffstage = false; bool _internalOffstage = false; + final double _scaleFactor; Orientation? _lastOrientation; // The Rect of the child at the moment that the CupertinoContextMenu opens. final Rect _previousChildRect; @@ -876,8 +902,8 @@ class _ContextMenuRoute extends PopupRoute { // to the original position before the bounce. final Rect childRectOriginal = Rect.fromCenter( center: _previousChildRect.center, - width: _previousChildRect.width / _kOpenScale, - height: _previousChildRect.height / _kOpenScale, + width: _previousChildRect.width / _scaleFactor, + height: _previousChildRect.height / _scaleFactor, ); final Rect sheetRect = _getRect(_sheetGlobalKey); diff --git a/packages/flutter/test/cupertino/context_menu_test.dart b/packages/flutter/test/cupertino/context_menu_test.dart index a9c9f7dd51..96bc3d5acb 100644 --- a/packages/flutter/test/cupertino/context_menu_test.dart +++ b/packages/flutter/test/cupertino/context_menu_test.dart @@ -12,6 +12,7 @@ import 'package:flutter_test/flutter_test.dart'; void main() { final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized(); const double kOpenScale = 1.15; + const double kMinScaleFactor = 1.02; Widget getChild() { return Container( @@ -690,6 +691,86 @@ void main() { } }); + testWidgets('CupertinoContextMenu minimizes scaling offscreen', (WidgetTester tester) async { + final Widget child = getChild(); + + // Pump a CupertinoContextMenu on the top-left of the screen and open it. + await tester.pumpWidget(getContextMenu( + alignment: Alignment.topLeft, + child: child, + )); + await tester.pump(); + Rect childRect = tester.getRect(find.byWidget(child)); + // Start a press on the child. + final TestGesture gesture1 = await tester.startGesture(childRect.center); + await tester.pump(); + + // The _DecoyChild is showing directly on top of the child. + expect(findDecoyChild(child), findsOneWidget); + Rect decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, equals(decoyChildRect)); + + expect(find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_DecoyChild'), findsOneWidget); + + // After a small delay, the _DecoyChild has begun to animate. + await tester.pump(const Duration(milliseconds: 400)); + decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, isNot(equals(decoyChildRect))); + + // Eventually the decoy fully scales. Since the context menu is fully + // top-left aligned, the minimum scale factor is used so that the menu + // animates minimally off the screen. + await tester.pump(const Duration(milliseconds: 900)); + decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, isNot(equals(decoyChildRect))); + expect(decoyChildRect.width, childRect.width * kMinScaleFactor); + + // Open and then close the CupertinoContextMenu. + await tester.pumpAndSettle(); + await tester.tapAt(const Offset(1.0, 1.0)); + await tester.pumpAndSettle(); + expect(findStatic(), findsNothing); + + // Pump a CupertinoContextMenu on the bottom-right of the screen and open it. + await tester.pumpWidget(getContextMenu( + alignment: Alignment.bottomRight, + child: child, + )); + await tester.pump(); + childRect = tester.getRect(find.byWidget(child)); + // Start a press on the child. + final TestGesture gesture2 = await tester.startGesture(childRect.center); + await tester.pump(); + + // The _DecoyChild is showing directly on top of the child. + expect(findDecoyChild(child), findsOneWidget); + decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, equals(decoyChildRect)); + + expect(find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_DecoyChild'), findsOneWidget); + + // After a small delay, the _DecoyChild has begun to animate. + await tester.pump(const Duration(milliseconds: 400)); + decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, isNot(equals(decoyChildRect))); + + // Eventually the decoy fully scales. Since the context menu is fully + // bottom-right aligned, the minimum scale factor is used so that the menu + // animates minimally off the screen. + await tester.pump(const Duration(milliseconds: 900)); + decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, isNot(equals(decoyChildRect))); + expect(decoyChildRect.width, childRect.width * kMinScaleFactor); + + // Open and then close the CupertinoContextMenu. + await tester.pumpAndSettle(); + await tester.tapAt(const Offset(1.0, 1.0)); + await tester.pumpAndSettle(); + expect(findStatic(), findsNothing); + await gesture1.up(); + await gesture2.up(); + }); + testWidgets("ContextMenu route animation doesn't throw exception on dismiss", (WidgetTester tester) async { // This is a regression test for https://github.com/flutter/flutter/issues/124597. final List items = List.generate(2, (int index) => index).toList();