dartpub.dev DartNative · beta
plugins / haptics_kit
ha

haptics_kit

v0.1.0 MIT

Haptic feedback for DartNative: success, warning, error, impacts and selection, with haptic_feedback's API.

Haptic feedback for DartNative apps with the haptic_feedback package's API: Haptics.canVibrate() and Haptics.vibrate(HapticsType) for success, warning, error, light, medium, heavy, rigid, soft and selection. iOS uses UIKit's impact and selection generators plus UINotificationFeedbackGenerator for success, warning and error. Android uses VibrationEffect, or HapticFeedbackConstants if you opt in, through Kotlin over JNI; DartNative's core HapticFeedback is a no-op there. Pure FFI, no platform channels. Android: the VIBRATE permission is merged from the plugin's manifest; minSdk 24. Nothing to add to Info.plist.

by AbdurahmanAlmehdi/haptics_kit · DartNative ≥ 3.0 · updated today
Install
Free
pubspec.yaml
dependencies:
  haptics_kit:
    hosted: https://dartpub.dev
    version: ^0.1.0
Weekly installs
0
Active apps
0
Rating
0.0 · 0
Open issues
0

haptics_kit

Haptic feedback for DartNative apps, with the haptic_feedback package's API, so code written for it ports by changing one import:

// import 'package:haptic_feedback/haptic_feedback.dart';
import 'package:haptics_kit/haptics_kit.dart';

Nine types — success, warning, error, light, medium, heavy, rigid, soft and selection — on iOS (UIKit feedback generators) and Android (Vibrator / VibrationEffect, optionally HapticFeedbackConstants). It is a pure-FFI plugin: no view, no method channel, no callbacks into Dart.

Why this package

DartNative's built-in HapticFeedback has only light/medium/heavy impacts, selection and vibrate, and it does nothing on Android. haptics_kit adds the notification types (success, warning, error), rigid and soft, canVibrate, and a real Android implementation that follows the iOS patterns.

iOS Android
iOS Android

Install

dependencies:
  haptics_kit:
    hosted: https://dartpub.dev
    version: ^0.1.0
dn pub get

dn pub get adds the plugin to lib/dartnative_plugin_registrant.dart. main() needs only the usual first line:

void main() {
  DartNativePluginRegistrant.registerAll();
  runApp(const HomeScreen());
}

Platform setup

  • iOS: nothing. No Info.plist key; the pod links UIKit and CoreHaptics. Minimum iOS 15.

  • Android: nothing to add by hand. The plugin's manifest declares

    <uses-permission android:name="android.permission.VIBRATE" />
    

    and it is merged into your app. It is a normal install-time permission, with no runtime prompt. Minimum API 24.

Use

import 'package:haptics_kit/haptics_kit.dart';

final canVibrate = await Haptics.canVibrate();
if (canVibrate) {
  await Haptics.vibrate(HapticsType.success);
}

// Android only: route through VibrationAttributes (API 33+), or prefer the
// system's HapticFeedbackConstants where one fits.
await Haptics.vibrate(
  HapticsType.heavy,
  usage: HapticsUsage.media,
  useAndroidHapticConstants: true,
);

vibrate throws HapticsException (code is unavailable, unknownType or nativeError) when the native side is missing or fails. A helper that caches canVibrate() and catches everything is the usual app-side wrapper.

What happens where

Situation canVibrate() vibrate(t)
iPhone with a Taptic Engine true plays; silent if System Haptics is off
iPad, iOS Simulator false runs, nothing felt
Android with a vibrator, API 30+ with primitive support true composition primitives
Android API 26+ with amplitude control true amplitude waveform
Android API 26+ without amplitude control, or API 24–25 true on/off waveform
Android without a vibrator false nothing (the constants path may still play)
Android emulator usually true nothing felt
usage on iOS or Android < 33; useAndroidHapticConstants on iOS — ignored
macOS / Linux / Windows / dn test host false nothing
Native side missing (registerAll() not called) false throws HapticsException('unavailable')

With useAndroidHapticConstants, success/error map to CONFIRM/REJECT (API 30+), light to VIRTUAL_KEY, medium to KEYBOARD_PRESS, heavy to CONTEXT_CLICK and selection to CLOCK_TICK; warning, rigid and soft, or no resumed activity, fall through to the vibrator. Some OEM builds mute USAGE_UNKNOWN vibrations when touch haptics are off; pass usage: if that matters.

Example

example/lib/main.dart has one button per type, canVibrate, "Play all", and on Android the constants switch and a usage picker.

cd example && dn pub get && dn run

Differences from haptic_feedback

  • HapticsException replaces PlatformException / MissingPluginException.
  • No platform interface; tests set Haptics.debugBackendOverride to a HapticsBackend fake instead.
  • iOS 15 minimum, so the pre-iOS-13 fallbacks and the private _feedbackSupportLevel lookup are gone.
  • Android: soft falls back to TICK on API 30 (upstream's check made that fallback unreachable), and the on/off waveform uses VibrationEffect on API 26+.

Credits & license

  • haptic_feedback 0.6.5 by Joachim Nohl, BSD-3-Clause. It is the source of the API and its docs, the iOS type-to-generator mapping, and the Android patterns (timings, amplitudes, primitives, HapticFeedbackConstants mapping) and fallback chain. They were reworked from method channels to FFI; those portions stay under BSD-3-Clause.
  • This package's own code: MIT, see LICENSE.
  • Full notices are in THIRD_PARTY_NOTICES, which ships with the package.