| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- import 'dart:io' show Platform;
- class HapticUtils {
- static bool get isSupported {
- try {
- return !kIsWeb && (Platform.isAndroid || Platform.isIOS);
- } catch (e) {
- return false;
- }
- }
- static Future<void> lightImpact() async {
- if (!isSupported) return;
-
- try {
- await HapticFeedback.lightImpact();
- } catch (e) {
- // Silently ignore haptic feedback errors
- }
- }
- static Future<void> mediumImpact() async {
- if (!isSupported) return;
-
- try {
- await HapticFeedback.mediumImpact();
- } catch (e) {
- // Silently ignore haptic feedback errors
- }
- }
- static Future<void> heavyImpact() async {
- if (!isSupported) return;
-
- try {
- await HapticFeedback.heavyImpact();
- } catch (e) {
- // Silently ignore haptic feedback errors
- }
- }
- static Future<void> vibrate({int duration = 50}) async {
- if (!isSupported) return;
-
- try {
- // Use appropriate haptic feedback based on duration
- if (duration <= 30) {
- await HapticFeedback.lightImpact();
- } else if (duration <= 60) {
- await HapticFeedback.mediumImpact();
- } else {
- await HapticFeedback.heavyImpact();
- }
- } catch (e) {
- // Silently ignore haptic feedback errors
- }
- }
- }
|