native_haptics 0.0.1
native_haptics: ^0.0.1 copied to clipboard
Advanced haptic feedback plugin for Flutter. Provides custom and platform-specific haptic patterns for Android and iOS.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:native_haptics/native_haptics.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _NativeHapticsPlugin = NativeHaptics();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _NativeHapticsPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Haptic example app')),
body: SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 24),
Text('Running on: $_platformVersion\n'),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.heavyImpact(),
child: const Text('Heavy Impact'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.mediumImpact(),
child: const Text('Medium Impact'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.lightImpact(),
child: const Text('Light Impact'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.selectionClick(),
child: const Text('Selection Click'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.success(),
child: const Text('Success Notification'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.warning(),
child: const Text('Warning Notification'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.error(),
child: const Text('Error Notification'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.bounce(),
child: const Text('Bounce'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.bounceOnce(),
child: const Text('Bounce Once'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.rampUpPattern(),
child: const Text('Ramp Up Pattern (Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.repeatingPattern(),
child: const Text('Repeating Pattern (Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.resist(),
child: const Text('Resist (Low Ticks, Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.expand(),
child: const Text('Expand (Rise & Fall, Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.wobble(),
child: const Text('Wobble (Spins, Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.wobbleOnce(),
child: const Text('Wobble once (Spins, Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.spring(),
child: const Text('Spring (Envelope, Android 14+)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.rocketLaunch(),
child: const Text(
'Rocket Launch (Waveform Envelope, Android 14+)',
),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.vibratePattern(
pattern: [50, 50, 50, 50, 50, 100, 350, 250],
amplitudes: [77, 79, 84, 99, 143, 255, 0, 255],
repeat: false,
),
child: const Text('Vibrate Pattern (Android)'),
),
ElevatedButton(
onPressed: () => _NativeHapticsPlugin.vibrateWithAmplitude(
duration: 300,
amplitude: 180,
),
child: const Text('Vibrate with Amplitude (Android)'),
),
const SizedBox(height: 24),
],
),
),
),
),
);
}
}