From 7176809583d684cb32c9b98c58f7ef7c24372fc7 Mon Sep 17 00:00:00 2001 From: Victor Sanni Date: Thu, 30 May 2024 16:00:21 -0700 Subject: [PATCH] Add feedback for long press on iOS (#148922) Adds the click system sound and heavy-impact haptic feedback to iOS on long presses. Fixes #148391 --- .../flutter/lib/src/widgets/feedback.dart | 20 +++++++++++-------- .../flutter/test/widgets/feedback_test.dart | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/widgets/feedback.dart b/packages/flutter/lib/src/widgets/feedback.dart index 2f01f8f9fb910..f0817ab75df68 100644 --- a/packages/flutter/lib/src/widgets/feedback.dart +++ b/packages/flutter/lib/src/widgets/feedback.dart @@ -20,9 +20,6 @@ import 'gesture_detector.dart'; /// [wrapForTap] or [wrapForLongPress] to achieve the same (see example code /// below). /// -/// Calling any of these methods is a no-op on iOS as actions on that platform -/// typically don't provide haptic or acoustic feedback. -/// /// All methods in this class are usually called from within a /// [StatelessWidget.build] method or from a [State]'s methods as you have to /// provide a [BuildContext]. @@ -127,8 +124,10 @@ abstract final class Feedback { /// Provides platform-specific feedback for a long press. /// - /// On Android the platform-typical vibration is triggered. On iOS this is a - /// no-op as that platform usually doesn't provide feedback for long presses. + /// On Android the platform-typical vibration is triggered. On iOS a + /// heavy-impact haptic feedback is triggered alongside the click system + /// sound, which was observed to be the default behavior on a physical iPhone + /// 15 Pro running iOS version 17.5. /// /// See also: /// @@ -141,6 +140,10 @@ abstract final class Feedback { case TargetPlatform.fuchsia: return HapticFeedback.vibrate(); case TargetPlatform.iOS: + return Future.wait(>[ + SystemSound.play(SystemSoundType.click), + HapticFeedback.heavyImpact() + ]); case TargetPlatform.linux: case TargetPlatform.macOS: case TargetPlatform.windows: @@ -151,9 +154,10 @@ abstract final class Feedback { /// Wraps a [GestureLongPressCallback] to provide platform specific feedback /// for a long press before the provided callback is executed. /// - /// On Android the platform-typical vibration is triggered. On iOS this - /// is a no-op as that platform usually doesn't provide feedback for a long - /// press. + /// On Android the platform-typical vibration is triggered. On iOS a + /// heavy-impact haptic feedback is triggered alongside the click system + /// sound, which was observed to be the default behavior on a physical iPhone + /// 15 Pro running iOS version 17.5. /// /// See also: /// diff --git a/packages/flutter/test/widgets/feedback_test.dart b/packages/flutter/test/widgets/feedback_test.dart index 2baade0bb95d4..e66bcc21ad6e5 100644 --- a/packages/flutter/test/widgets/feedback_test.dart +++ b/packages/flutter/test/widgets/feedback_test.dart @@ -190,8 +190,8 @@ void main () { await tester.longPress(find.text('X')); await tester.pumpAndSettle(kWaitDuration); - expect(feedback.hapticCount, 0); - expect(feedback.clickSoundCount, 0); + expect(feedback.hapticCount, 1); + expect(feedback.clickSoundCount, 1); }, variant: TargetPlatformVariant.only(TargetPlatform.iOS)); });