diff --git a/packages/flutter/lib/src/cupertino/context_menu.dart b/packages/flutter/lib/src/cupertino/context_menu.dart index 1545c73b3c..52fc841569 100644 --- a/packages/flutter/lib/src/cupertino/context_menu.dart +++ b/packages/flutter/lib/src/cupertino/context_menu.dart @@ -8,6 +8,7 @@ import 'dart:ui' as ui; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart' show kMinFlingVelocity; import 'package:flutter/scheduler.dart'; +import 'package:flutter/services.dart' show HapticFeedback; import 'package:flutter/widgets.dart'; import 'colors.dart'; @@ -137,6 +138,7 @@ class CupertinoContextMenu extends StatefulWidget { super.key, required this.actions, required Widget this.child, + this.enableHapticFeedback = false, @Deprecated( 'Use CupertinoContextMenu.builder instead. ' 'This feature was deprecated after v3.4.0-34.1.pre.', @@ -157,6 +159,7 @@ class CupertinoContextMenu extends StatefulWidget { super.key, required this.actions, required this.builder, + this.enableHapticFeedback = false, }) : assert(actions.isNotEmpty), child = null, previewBuilder = null; @@ -388,6 +391,13 @@ class CupertinoContextMenu extends StatefulWidget { /// This parameter cannot be null or empty. final List actions; + /// If true, clicking on the [CupertinoContextMenuAction]s will + /// produce haptic feedback. + /// + /// Uses [HapticFeedback.heavyImpact] when activated. + /// Defaults to false. + final bool enableHapticFeedback; + /// A function that returns an alternative widget to show when the /// [CupertinoContextMenu] is open. /// @@ -481,6 +491,15 @@ class _CupertinoContextMenuState extends State with Ticker _openController.addStatusListener(_onDecoyAnimationStatusChange); } + void _listenerCallback() { + if (_openController.status != AnimationStatus.reverse && + _openController.value >= _midpoint && + widget.enableHapticFeedback) { + HapticFeedback.heavyImpact(); + _openController.removeListener(_listenerCallback); + } + } + // Determine the _ContextMenuLocation based on the location of the original // child in the screen. // @@ -583,24 +602,28 @@ class _CupertinoContextMenuState extends State with Ticker } void _onTap() { + _openController.removeListener(_listenerCallback); if (_openController.isAnimating && _openController.value < _midpoint) { _openController.reverse(); } } void _onTapCancel() { + _openController.removeListener(_listenerCallback); if (_openController.isAnimating && _openController.value < _midpoint) { _openController.reverse(); } } void _onTapUp(TapUpDetails details) { + _openController.removeListener(_listenerCallback); if (_openController.isAnimating && _openController.value < _midpoint) { _openController.reverse(); } } void _onTapDown(TapDownDetails details) { + _openController.addListener(_listenerCallback); setState(() { _childHidden = true; }); @@ -1370,27 +1393,24 @@ class _ContextMenuRouteStaticState extends State<_ContextMenuRouteStatic> with T ); return SafeArea( - child: Padding( - padding: const EdgeInsets.all(_kPadding), - child: Align( - alignment: Alignment.topLeft, - child: GestureDetector( - onPanEnd: _onPanEnd, - onPanStart: _onPanStart, - onPanUpdate: _onPanUpdate, - child: AnimatedBuilder( - animation: _moveController, - builder: _buildAnimation, - child: widget.orientation == Orientation.portrait - ? Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: children, - ) - : Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: children, - ), - ), + child: Align( + alignment: Alignment.topLeft, + child: GestureDetector( + onPanEnd: _onPanEnd, + onPanStart: _onPanStart, + onPanUpdate: _onPanUpdate, + child: AnimatedBuilder( + animation: _moveController, + builder: _buildAnimation, + child: widget.orientation == Orientation.portrait + ? Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: children, + ) + : Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: children, + ), ), ), ), @@ -1414,12 +1434,13 @@ class _ContextMenuSheet extends StatelessWidget { final _ContextMenuLocation _contextMenuLocation; final Orientation _orientation; + static const double _kMenuWidth = 250.0; + // Get the children, whose order depends on orientation and // contextMenuLocation. List getChildren(BuildContext context) { - final Widget menu = Flexible( - fit: FlexFit.tight, - flex: 2, + final Widget menu = SizedBox( + width: _kMenuWidth, child: IntrinsicHeight( child: ClipRRect( borderRadius: const BorderRadius.all(Radius.circular(13.0)), @@ -1432,9 +1453,12 @@ class _ContextMenuSheet extends StatelessWidget { decoration: BoxDecoration( border: Border( top: BorderSide( - color: CupertinoDynamicColor.resolve(_borderColor, context), - width: 0.5, - ) + color: CupertinoDynamicColor.resolve( + _borderColor, + context, + ), + width: 0.4, + ), ), ), position: DecorationPosition.foreground, diff --git a/packages/flutter/lib/src/cupertino/context_menu_action.dart b/packages/flutter/lib/src/cupertino/context_menu_action.dart index 4c26071f2b..d55d711a34 100644 --- a/packages/flutter/lib/src/cupertino/context_menu_action.dart +++ b/packages/flutter/lib/src/cupertino/context_menu_action.dart @@ -48,18 +48,18 @@ class CupertinoContextMenuAction extends StatefulWidget { class _CupertinoContextMenuActionState extends State { static const Color _kBackgroundColor = CupertinoDynamicColor.withBrightness( - color: Color(0xFFEEEEEE), + color: Color(0xFFF1F1F1), darkColor: Color(0xFF212122), ); static const Color _kBackgroundColorPressed = CupertinoDynamicColor.withBrightness( color: Color(0xFFDDDDDD), darkColor: Color(0xFF3F3F40), ); - static const double _kButtonHeight = 56.0; + static const double _kButtonHeight = 43; static const TextStyle _kActionSheetActionStyle = TextStyle( fontFamily: '.SF UI Text', inherit: false, - fontSize: 20.0, + fontSize: 16.0, fontWeight: FontWeight.w400, color: CupertinoColors.black, textBaseline: TextBaseline.alphabetic, @@ -125,9 +125,11 @@ class _CupertinoContextMenuActionState extends State ? CupertinoDynamicColor.resolve(_kBackgroundColorPressed, context) : CupertinoDynamicColor.resolve(_kBackgroundColor, context), ), - padding: const EdgeInsets.symmetric( - vertical: 16.0, - horizontal: 10.0, + padding: const EdgeInsets.only( + top: 8, + bottom: 8, + left: 15.5, + right: 17.5, ), child: DefaultTextStyle( style: _textStyle, @@ -141,6 +143,7 @@ class _CupertinoContextMenuActionState extends State Icon( widget.trailingIcon, color: _textStyle.color, + size: 21.0, ), ], ), diff --git a/packages/flutter/test/cupertino/context_menu_action_test.dart b/packages/flutter/test/cupertino/context_menu_action_test.dart index de09eb1065..26c8dd78ec 100644 --- a/packages/flutter/test/cupertino/context_menu_action_test.dart +++ b/packages/flutter/test/cupertino/context_menu_action_test.dart @@ -12,11 +12,13 @@ import '../rendering/mock_canvas.dart'; void main() { // Constants taken from _ContextMenuActionState. - const CupertinoDynamicColor kBackgroundColor = CupertinoDynamicColor.withBrightness( - color: Color(0xFFEEEEEE), + const CupertinoDynamicColor kBackgroundColor = + CupertinoDynamicColor.withBrightness( + color: Color(0xFFF1F1F1), darkColor: Color(0xFF212122), ); - const CupertinoDynamicColor kBackgroundColorPressed = CupertinoDynamicColor.withBrightness( + const CupertinoDynamicColor kBackgroundColorPressed = + CupertinoDynamicColor.withBrightness( color: Color(0xFFDDDDDD), darkColor: Color(0xFF3F3F40), ); @@ -84,69 +86,85 @@ void main() { testWidgets('turns grey when pressed and held', (WidgetTester tester) async { await tester.pumpWidget(getApp()); - expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.color)); + expect(find.byType(CupertinoContextMenuAction), + paints..rect(color: kBackgroundColor.color)); - final Offset actionCenterLight = tester.getCenter(find.byType(CupertinoContextMenuAction)); - final TestGesture gestureLight = await tester.startGesture(actionCenterLight); + final Offset actionCenterLight = + tester.getCenter(find.byType(CupertinoContextMenuAction)); + final TestGesture gestureLight = + await tester.startGesture(actionCenterLight); await tester.pump(); - expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColorPressed.color)); + expect(find.byType(CupertinoContextMenuAction), + paints..rect(color: kBackgroundColorPressed.color)); await gestureLight.up(); await tester.pump(); - expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.color)); + expect(find.byType(CupertinoContextMenuAction), + paints..rect(color: kBackgroundColor.color)); await tester.pumpWidget(getApp(brightness: Brightness.dark)); - expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.darkColor)); + expect(find.byType(CupertinoContextMenuAction), + paints..rect(color: kBackgroundColor.darkColor)); - final Offset actionCenterDark = tester.getCenter(find.byType(CupertinoContextMenuAction)); + final Offset actionCenterDark = + tester.getCenter(find.byType(CupertinoContextMenuAction)); final TestGesture gestureDark = await tester.startGesture(actionCenterDark); await tester.pump(); - expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColorPressed.darkColor)); + expect(find.byType(CupertinoContextMenuAction), + paints..rect(color: kBackgroundColorPressed.darkColor)); await gestureDark.up(); await tester.pump(); - expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.darkColor)); + expect(find.byType(CupertinoContextMenuAction), + paints..rect(color: kBackgroundColor.darkColor)); }); - testWidgets('icon and textStyle colors are correct out of the box', (WidgetTester tester) async { + testWidgets('icon and textStyle colors are correct out of the box', + (WidgetTester tester) async { await tester.pumpWidget(getApp()); expect(getTextStyle(tester).color, CupertinoColors.label); - expect(getIcon(tester).color, CupertinoColors.label); + expect(getIcon(tester).color, CupertinoColors.label); }); - testWidgets('icon and textStyle colors are correct for destructive actions', (WidgetTester tester) async { + testWidgets('icon and textStyle colors are correct for destructive actions', + (WidgetTester tester) async { await tester.pumpWidget(getApp(isDestructiveAction: true)); expect(getTextStyle(tester).color, kDestructiveActionColor); expect(getIcon(tester).color, kDestructiveActionColor); }); - testWidgets('textStyle is correct for defaultAction', (WidgetTester tester) async { + testWidgets('textStyle is correct for defaultAction', + (WidgetTester tester) async { await tester.pumpWidget(getApp(isDefaultAction: true)); expect(getTextStyle(tester).fontWeight, kDefaultActionWeight); }); testWidgets( - 'Hovering over Cupertino context menu action updates cursor to clickable on Web', - (WidgetTester tester) async { - /// Cupertino context menu action without "onPressed" callback. - await tester.pumpWidget(getApp()); - final Offset contextMenuAction = tester.getCenter(find.text('I am a CupertinoContextMenuAction')); - final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1); - await gesture.addPointer(location: contextMenuAction); - await tester.pumpAndSettle(); - expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); + 'Hovering over Cupertino context menu action updates cursor to clickable on Web', + (WidgetTester tester) async { + /// Cupertino context menu action without "onPressed" callback. + await tester.pumpWidget(getApp()); + final Offset contextMenuAction = + tester.getCenter(find.text('I am a CupertinoContextMenuAction')); + final TestGesture gesture = + await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1); + await gesture.addPointer(location: contextMenuAction); + await tester.pumpAndSettle(); + expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), + SystemMouseCursors.basic); - // / Cupertino context menu action with "onPressed" callback. - await tester.pumpWidget(getApp(onPressed: (){})); - await gesture.moveTo(const Offset(10, 10)); - await tester.pumpAndSettle(); - expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); + // / Cupertino context menu action with "onPressed" callback. + await tester.pumpWidget(getApp(onPressed: () {})); + await gesture.moveTo(const Offset(10, 10)); + await tester.pumpAndSettle(); + expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), + SystemMouseCursors.basic); - await gesture.moveTo(contextMenuAction); - await tester.pumpAndSettle(); - expect( - RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), - kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic, - ); + await gesture.moveTo(contextMenuAction); + await tester.pumpAndSettle(); + expect( + RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), + kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic, + ); }); } diff --git a/packages/flutter/test/cupertino/context_menu_test.dart b/packages/flutter/test/cupertino/context_menu_test.dart index c28dc5c33d..8a90d8740c 100644 --- a/packages/flutter/test/cupertino/context_menu_test.dart +++ b/packages/flutter/test/cupertino/context_menu_test.dart @@ -583,6 +583,49 @@ void main() { final ClipRRect previewWidget = tester.firstWidget(findStaticDefaultPreview()) as ClipRRect; expect(previewWidget.borderRadius, equals(BorderRadius.circular(12.0))); }); + + testWidgets('CupertinoContextMenu width is correct', (WidgetTester tester) async { + final Widget child = getChild(); + await tester.pumpWidget(getContextMenu(child: child)); + expect(find.byWidget(child), findsOneWidget); + final Rect childRect = tester.getRect(find.byWidget(child)); + expect(find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_DecoyChild'), findsNothing); + + // Start a press on the child. + final TestGesture gesture = 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 by _kOpenSize. + await tester.pump(const Duration(milliseconds: 800)); + decoyChildRect = tester.getRect(findDecoyChild(child)); + expect(childRect, isNot(equals(decoyChildRect))); + expect(decoyChildRect.width, childRect.width * kOpenScale); + + // Then the CupertinoContextMenu opens. + await tester.pumpAndSettle(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(findStatic(), findsOneWidget); + + // The CupertinoContextMenu has the correct width and height. + final CupertinoContextMenu widget = tester.widget(find.byType(CupertinoContextMenu)); + for (final Widget action in widget.actions) { + // The value of the height is 80 because of the font and icon size. + expect(tester.getSize(find.byWidget(action)).width, 250); + } + }); }); group("Open layout differs depending on child's position on screen", () {