Skip to content

Commit 265d0d3

Browse files
authored
Add more haptic feedback varieties (flutter#14829)
* Add more haptic feedback varieties * tests * un-meta-ify the test
1 parent ff04e79 commit 265d0d3

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

packages/flutter/lib/src/services/haptic_feedback.dart

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,70 @@ import 'system_channels.dart';
1313
class HapticFeedback {
1414
HapticFeedback._();
1515

16-
/// Provides haptic feedback to the user for a short duration.
16+
/// Provides vibration haptic feedback to the user for a short duration.
1717
///
1818
/// On iOS devices that support haptic feedback, this uses the default system
1919
/// vibration value (`kSystemSoundID_Vibrate`).
2020
///
2121
/// On Android, this uses the platform haptic feedback API to simulate a
22-
/// short tap on a virtual keyboard.
22+
/// response to a long press (`HapticFeedbackConstants.LONG_PRESS`).
2323
static Future<Null> vibrate() async {
2424
await SystemChannels.platform.invokeMethod('HapticFeedback.vibrate');
2525
}
26+
27+
/// Provides a haptic feedback corresponding a collision impact with a light mass.
28+
///
29+
/// On iOS versions 10 and above, this uses a `UIImpactFeedbackGenerator` with
30+
/// `UIImpactFeedbackStyleLight`. This call has no effects on iOS versions
31+
/// below 10.
32+
///
33+
/// On Android, this uses `HapticFeedbackConstants.VIRTUAL_KEY`.
34+
static Future<Null> lightImpact() async {
35+
await SystemChannels.platform.invokeMethod(
36+
'HapticFeedback.vibrate',
37+
'HapticFeedbackType.lightImpact',
38+
);
39+
}
40+
41+
/// Provides a haptic feedback corresponding a collision impact with a medium mass.
42+
///
43+
/// On iOS versions 10 and above, this uses a `UIImpactFeedbackGenerator` with
44+
/// `UIImpactFeedbackStyleMedium`. This call has no effects on iOS versions
45+
/// below 10.
46+
///
47+
/// On Android, this uses `HapticFeedbackConstants.KEYBOARD_TAP`.
48+
static Future<Null> mediumImpact() async {
49+
await SystemChannels.platform.invokeMethod(
50+
'HapticFeedback.vibrate',
51+
'HapticFeedbackType.mediumImpact',
52+
);
53+
}
54+
55+
/// Provides a haptic feedback corresponding a collision impact with a heavy mass.
56+
///
57+
/// On iOS versions 10 and above, this uses a `UIImpactFeedbackGenerator` with
58+
/// `UIImpactFeedbackStyleHeavy`. This call has no effects on iOS versions
59+
/// below 10.
60+
///
61+
/// On Android, this uses `HapticFeedbackConstants.CONTEXT_CLICK` on API levels
62+
/// 23 and above. This call has no effects on Android API levels below 23.
63+
static Future<Null> heavyImpact() async {
64+
await SystemChannels.platform.invokeMethod(
65+
'HapticFeedback.vibrate',
66+
'HapticFeedbackType.heavyImpact',
67+
);
68+
}
69+
70+
/// Provides a haptic feedback indication selection changing through discrete values.
71+
///
72+
/// On iOS versions 10 and above, this uses a `UISelectionFeedbackGenerator`.
73+
/// This call has no effects on iOS versions below 10.
74+
///
75+
/// On Android, this uses `HapticFeedbackConstants.CLOCK_TICK`.
76+
static Future<Null> selectionClick() async {
77+
await SystemChannels.platform.invokeMethod(
78+
'HapticFeedback.vibrate',
79+
'HapticFeedbackType.selectionClick',
80+
);
81+
}
2682
}

packages/flutter/test/services/haptic_feedback_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,26 @@ void main() {
1818
expect(log, hasLength(1));
1919
expect(log.single, isMethodCall('HapticFeedback.vibrate', arguments: null));
2020
});
21+
22+
test('Haptic feedback variation tests', () async {
23+
Future<void> callAndVerifyHapticFunction(Function hapticFunction, String platformMethodArgument) async {
24+
final List<MethodCall> log = <MethodCall>[];
25+
26+
SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async {
27+
log.add(methodCall);
28+
});
29+
30+
await Function.apply(hapticFunction, null);
31+
expect(log, hasLength(1));
32+
expect(
33+
log.last,
34+
isMethodCall('HapticFeedback.vibrate', arguments: platformMethodArgument),
35+
);
36+
}
37+
38+
await callAndVerifyHapticFunction(HapticFeedback.lightImpact, 'HapticFeedbackType.lightImpact');
39+
await callAndVerifyHapticFunction(HapticFeedback.mediumImpact, 'HapticFeedbackType.mediumImpact');
40+
await callAndVerifyHapticFunction(HapticFeedback.heavyImpact, 'HapticFeedbackType.heavyImpact');
41+
await callAndVerifyHapticFunction(HapticFeedback.selectionClick, 'HapticFeedbackType.selectionClick');
42+
});
2143
}

0 commit comments

Comments
 (0)