update flutter_vibrate

This commit is contained in:
2026-01-13 23:09:42 -08:00
parent 2a3652d808
commit 12fb3a2089
8 changed files with 474 additions and 97 deletions
@@ -4,34 +4,60 @@ import 'src/messages.g.dart';
export 'src/messages.g.dart' show VibrateApi;
/// A set of feedback types that can be used to provide haptic feedback.
enum FeedbackType {
/// Indicates a task has completed successfully.
success,
/// Indicates a task has failed or an error occurred.
error,
/// Indicates a warning or caution.
warning,
/// Indicates a selection change (e.g., scrolling through a list).
selection,
/// Provides a heavy physical impact.
impact,
/// Provides a heavy vibration.
heavy,
/// Provides a medium vibration.
medium,
/// Provides a light vibration.
light,
}
/// A class that provides access to the device's vibration hardware.
class Vibrate {
static final VibrateApi _api = VibrateApi();
static const Duration _defaultVibrationDuration = Duration(milliseconds: 500);
/// Vibrate for 500ms on Android, and for the default time on iOS (about 500ms as well)
/// Vibrates the device for 500ms on Android, and for the default system vibration duration on iOS.
static Future<void> vibrate() async {
await _api.vibrate(500);
}
/// Whether the device can actually vibrate or not
/// Checks if the device has vibration hardware.
///
/// Returns `true` if the device can vibrate, `false` otherwise.
static Future<bool> get canVibrate async {
return _api.canVibrate();
}
/// Vibrates with [pauses] in between each vibration
/// Will always vibrate once before the first pause
/// and once after the last pause
/// Vibrates the device with a specific pattern of pauses.
///
/// The [pauses] iterable defines the duration of silence in between vibrations.
/// The pattern will always start with a vibration, followed by the first pause,
/// then another vibration, and so on, ending with a final vibration.
///
/// For example, if [pauses] is `[Duration(seconds: 1)]`, the device will:
/// 1. Vibrate (default duration)
/// 2. Wait for 1 second
/// 3. Vibrate (default duration)
static Future<void> vibrateWithPauses(Iterable<Duration> pauses) async {
for (final Duration d in pauses) {
await vibrate();
@@ -44,6 +70,10 @@ class Vibrate {
await vibrate();
}
/// Provides haptic feedback corresponding to the specified [type].
///
/// This uses the platform's native haptic feedback mechanisms (e.g., `UINotificationFeedbackGenerator` on iOS,
/// `View.performHapticFeedback` on Android).
static Future<void> feedback(FeedbackType type) async {
switch (type) {
case FeedbackType.impact: