update flutter_vibrate
This commit is contained in:
@@ -1,68 +1,122 @@
|
||||
[](https://www.buymeacoffee.com/rodydavis)
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WSH3GVC49GNNJ)
|
||||

|
||||
[](https://github.com/rodydavis/flutter_vibrate)
|
||||
[](https://pub.dev/packages/flutter_vibrate)
|
||||
# Flutter Vibrate
|
||||
[](https://pub.dev/packages/flutter_vibrate)
|
||||
|
||||
# Vibrate
|
||||
A Flutter plugin to provide haptic feedback on iOS and Android. This package unifies the haptic feedback APIs, allowing for consistent vibration patterns and feedback types across both platforms.
|
||||
|
||||
A Flutter plugin to vibrate the device.
|
||||
This uses all the current Haptic Feedback APIs from Apple and provides similar feedback on Android.
|
||||
| Platform | Support |
|
||||
| :--- | :---: |
|
||||
| Android | ✅ |
|
||||
| iOS | ✅ |
|
||||
| macOS | ❌ |
|
||||
| Web | ❌ |
|
||||
| Linux | ❌ |
|
||||
| Windows | ❌ |
|
||||
|
||||
## Features
|
||||
|
||||
- **Device Vibration**: Trigger standard vibrations or custom patterns (Android).
|
||||
- **Haptic Feedback**: Access platform-specific haptic feedback constants (Impact, Selection, Success, Warning, Error, etc.).
|
||||
- **Type Safety**: Built with Pigeon for type-safe communication between Flutter and native platforms.
|
||||
- **Modern Android Support**: Uses `View.performHapticFeedback` for broad compatibility and adheres to modern Android haptic standards.
|
||||
- **iOS Haptics**: Uses `UIImpactFeedbackGenerator` and `UINotificationFeedbackGenerator` for rich haptic experiences on iOS 10+.
|
||||
|
||||
## Getting Started
|
||||
|
||||
Make sure you add the following permissions to your Android Manifest
|
||||
``` xml
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
### Installation
|
||||
|
||||
Add `flutter_vibrate` to your `pubspec.yaml`:
|
||||
|
||||
```yaml
|
||||
dependencies:
|
||||
flutter_vibrate: ^1.4.0
|
||||
```
|
||||
|
||||
### Android Setup
|
||||
|
||||
Add the vibration permission to your `AndroidManifest.xml` (`android/app/src/main/AndroidManifest.xml`):
|
||||
|
||||
```xml
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
</manifest>
|
||||
```
|
||||
|
||||
> **Note**: While `vibrate()` requires this permission, many haptic feedback types (like selection, impact) may work without it on some Android versions using the View-based API. However, it is recommended to include it for full functionality.
|
||||
|
||||
### iOS Setup
|
||||
|
||||
No additional configuration is required. The plugin uses `AudioServicesPlaySystemSound` and `UIFeedbackGenerator` which are available by default.
|
||||
|
||||
## Usage
|
||||
``` dart
|
||||
// Import package
|
||||
|
||||
Import the package:
|
||||
|
||||
```dart
|
||||
import 'package:flutter_vibrate/flutter_vibrate.dart';
|
||||
```
|
||||
|
||||
### Vibration
|
||||
``` dart
|
||||
### Basic Vibration
|
||||
|
||||
Check for device capabilities and vibrate:
|
||||
|
||||
```dart
|
||||
// Check if the device can vibrate
|
||||
bool canVibrate = await Vibrate.canVibrate;
|
||||
|
||||
// Vibrate
|
||||
// Vibration duration is a constant 500ms because
|
||||
// it cannot be set to a specific duration on iOS.
|
||||
Vibrate.vibrate();
|
||||
if (canVibrate) {
|
||||
// Vibrate for 500ms
|
||||
Vibrate.vibrate();
|
||||
}
|
||||
```
|
||||
|
||||
// Vibrate with pauses between each vibration
|
||||
final Iterable<Duration> pauses = [
|
||||
const Duration(milliseconds: 500),
|
||||
const Duration(milliseconds: 1000),
|
||||
const Duration(milliseconds: 500),
|
||||
### Vibration Patterns
|
||||
|
||||
You can create custom patterns by specifying a list of pauses. The pattern alternates between vibrating and pausing.
|
||||
|
||||
> **Android**: Supports custom patterns with variable pauses.
|
||||
>
|
||||
> **iOS**: The OS does not support fine-grained custom patterns. This method will vibrate once for each interval in the list.
|
||||
|
||||
```dart
|
||||
final Iterable<Duration> pauses = const [
|
||||
Duration(milliseconds: 500), // Vibrate
|
||||
Duration(milliseconds: 1000), // Wait
|
||||
Duration(milliseconds: 500), // Vibrate
|
||||
];
|
||||
// vibrate - sleep 0.5s - vibrate - sleep 1s - vibrate - sleep 0.5s - vibrate
|
||||
|
||||
Vibrate.vibrateWithPauses(pauses);
|
||||
```
|
||||
|
||||
### Haptic Feedback
|
||||
``` dart
|
||||
// Choose from any of these available methods
|
||||
enum FeedbackType {
|
||||
success,
|
||||
error,
|
||||
warning,
|
||||
selection,
|
||||
impact,
|
||||
heavy,
|
||||
medium,
|
||||
light
|
||||
}
|
||||
|
||||
var _type = FeedbackType.impact;
|
||||
Vibrate.feedback(_type);
|
||||
Trigger specific haptic feedback types to enhance user interaction:
|
||||
|
||||
```dart
|
||||
// Impact (light collision)
|
||||
Vibrate.feedback(FeedbackType.impact);
|
||||
|
||||
// Selection (scroll tick)
|
||||
Vibrate.feedback(FeedbackType.selection);
|
||||
|
||||
// Success (task completion)
|
||||
Vibrate.feedback(FeedbackType.success);
|
||||
|
||||
// Warning (potential issue)
|
||||
Vibrate.feedback(FeedbackType.warning);
|
||||
|
||||
// Error (task failure)
|
||||
Vibrate.feedback(FeedbackType.error);
|
||||
|
||||
// Heavy Impact
|
||||
Vibrate.feedback(FeedbackType.heavy);
|
||||
|
||||
// Medium Impact
|
||||
Vibrate.feedback(FeedbackType.medium);
|
||||
|
||||
// Light Impact
|
||||
Vibrate.feedback(FeedbackType.light);
|
||||
```
|
||||
## Documentation
|
||||
#### Android
|
||||
|
||||
https://developer.android.com/reference/android/view/HapticFeedbackConstants
|
||||
|
||||
#### iOS
|
||||
|
||||
https://developer.apple.com/design/human-interface-guidelines/ios/user-interaction/feedback/
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! If you find a bug or want to add a feature, please file an issue or submit a pull request.
|
||||
|
||||
Reference in New Issue
Block a user