How to implement vibration with Flutter for both Android and iOS?

前端 未结 4 2732
臣服心动
臣服心动 2021-02-20 17:29

Using Flutter I am trying to implement vibration on a button click.

I find it surprisingly difficult to be honest. I\'ve tried using the following packages unsuccessfully

4条回答
  •  生来不讨喜
    2021-02-20 18:28

    I will recommend to use flutter_vibrate, it will work for both ios/ android. Simplier setup and works very well.

    1. Just add flutter_vibrate as a dependency in your pubspec.yaml file.

      dependencies: vibration: ^1.4.0

    2. Make sure you add the following permissions to your Android Manifest

    3. Import the package:

      import 'package:vibration/vibration.dart';

    Examples:

    Vibrate for default 500ms:

    Vibration.vibrate();
           
    

    Vibrate for 1000ms:

    Vibration.vibrate(duration: 1000);
               
    

    Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s:

    Vibration.vibrate(pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500]);
                
    

    Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s':

    Vibration.vibrate( pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500], intensities: [128, 255, 64, 255]);
    

    Android

    The VIBRATE permission is required in AndroidManifest.xml.

    Supports vibration with duration and pattern. On Android 8 (Oreo) and above, uses the [VibrationEffect][1] class. For the rest of the usage instructions, see [Vibrator][1] class documentation.

    iOS

    Supports vibration with duration and pattern on CoreHaptics devices. On older devices, the pattern is emulated with 500ms long vibrations. You can check whether the current device has CoreHaptics support using hasCustomVibrationsSupport.

提交回复
热议问题