vibrate_short_handler.dart 847 Bytes
import 'package:appframe/services/dispatcher.dart';
import 'package:vibration/vibration.dart';

class VibrateShortHandler extends MessageHandler {
  @override
  Future<dynamic> handleMessage(params) async {
    if (params != null && params is! Map<String, dynamic>) {
      throw Exception('参数错误');
    }

    int? duration;
    int? amplitude;
    if (params != null) {
      var type = params['type'] as String?;
      type ??= 'light';

      duration = params['duration'] as int?;
      duration ??= 15;

      if (type == 'light') {
        amplitude = 10;
      } else if (type == 'medium') {
        amplitude = 125;
      } else if (type == 'heavy') {
        amplitude = 250;
      } else {
        amplitude = 10;
      }
    }

    Vibration.vibrate(duration: duration ?? 15, amplitude: amplitude ?? 10);
    return true;
  }
}