m3_lightmeter/lib/data/haptics_service.dart
2024-05-06 14:14:50 +02:00

31 lines
849 B
Dart

import 'dart:io';
import 'package:vibration/vibration.dart';
class HapticsService {
const HapticsService();
Future<void> quickVibration() async => _tryVibrate(duration: 25, amplitude: 96);
Future<void> responseVibration() async => _tryVibrate(duration: 50, amplitude: 128);
Future<void> errorVibration() async => _tryVibrate(duration: 500, amplitude: 128);
Future<void> _tryVibrate({required int duration, required int amplitude}) async {
if (await _canVibrate()) {
if (Platform.isAndroid) {
await Vibration.vibrate(
duration: duration,
amplitude: amplitude,
);
} else {
await Vibration.vibrate(
pattern: [duration],
intensities: [amplitude],
);
}
}
}
Future<bool> _canVibrate() async => await Vibration.hasVibrator() ?? false;
}