2024-02-12 14:41:19 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-01-21 10:37:49 +00:00
|
|
|
import 'package:vibration/vibration.dart';
|
|
|
|
|
|
|
|
class HapticsService {
|
2023-01-22 19:30:29 +00:00
|
|
|
const HapticsService();
|
2023-01-26 15:03:48 +00:00
|
|
|
|
2023-01-21 10:37:49 +00:00
|
|
|
Future<void> quickVibration() async => _tryVibrate(duration: 25, amplitude: 96);
|
|
|
|
|
|
|
|
Future<void> responseVibration() async => _tryVibrate(duration: 50, amplitude: 128);
|
|
|
|
|
2023-05-16 09:47:53 +00:00
|
|
|
Future<void> errorVibration() async => _tryVibrate(duration: 100, amplitude: 128);
|
|
|
|
|
2023-01-21 10:37:49 +00:00
|
|
|
Future<void> _tryVibrate({required int duration, required int amplitude}) async {
|
|
|
|
if (await _canVibrate()) {
|
2024-02-12 14:41:19 +00:00
|
|
|
if (Platform.isAndroid) {
|
|
|
|
await Vibration.vibrate(
|
|
|
|
duration: duration,
|
|
|
|
amplitude: amplitude,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await Vibration.vibrate(
|
|
|
|
pattern: [duration],
|
|
|
|
intensities: [amplitude],
|
|
|
|
);
|
|
|
|
}
|
2023-01-21 10:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _canVibrate() async => await Vibration.hasVibrator() ?? false;
|
|
|
|
}
|