Skip to content

Commit

Permalink
Add feedback for long press on iOS (flutter#148922)
Browse files Browse the repository at this point in the history
Adds the click system sound and heavy-impact haptic feedback to iOS on long presses.

Fixes flutter#148391
  • Loading branch information
victorsanni committed May 31, 2024
1 parent 1f1768c commit 7176809
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions packages/flutter/lib/src/widgets/feedback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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:
///
Expand All @@ -141,6 +140,10 @@ abstract final class Feedback {
case TargetPlatform.fuchsia:
return HapticFeedback.vibrate();
case TargetPlatform.iOS:
return Future.wait(<Future<void>>[
SystemSound.play(SystemSoundType.click),
HapticFeedback.heavyImpact()
]);
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
Expand All @@ -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:
///
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/test/widgets/feedback_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down

0 comments on commit 7176809

Please sign in to comment.