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

前端 未结 4 2642
臣服心动
臣服心动 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:11
    import 'package:flutter/material.dart';
    import 'package:vibrate/vibrate.dart';
    
    // Note:
    // Make sure you add the following permissions to your Android Manifest
    // <uses-permission android:name="android.permission.VIBRATE"/>
    // 
    // In pubspec.yaml file, add following dependency
    // dependencies:
    //   vibrate: ^0.0.4
    
    class TestVibration extends StatefulWidget {
      @override
      _TestVibrationState createState() => _TestVibrationState();
    }
    
    class _TestVibrationState extends State<TestVibration> {
    
      bool canVibrate = false;
      @override
      void initState() {
        super.initState();
        _checkIfVibrate();
      }
      _checkIfVibrate() async {
        // check if device can vibrate
        canVibrate = await Vibrate.canVibrate;
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: RaisedButton(
            child: Text('Vibrate'),
            onPressed: (){
              // FeedbackTypes -> {success, error, warning, selection, impact, heavy, medium, light}
              _getVibration(FeedbackType.warning);
            },
          ),
        );
      }
    
      _getVibration(feedbackType) async {
        if (canVibrate) {
          Vibrate.feedback(feedbackType);
          // Vibrate.vibrate();   // Try this too!
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-20 18:15

    First, you need to add vibrate: as a dependency to your pubspec.yaml file.

    dependencies:
      flutter:
        sdk: flutter
      vibrate:
    

    After this, you need to import the package in the class that you are using.

    // Import package
    import 'package:vibrate/vibrate.dart';
    

    Now you can this for vibration:

    // 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();
    
    // Vibrate with pauses between each vibration
    final Iterable<Duration> pauses = [
        const Duration(milliseconds: 500),
        const Duration(milliseconds: 1000),
        const Duration(milliseconds: 500),
    ];
    // vibrate - sleep 0.5s - vibrate - sleep 1s - vibrate - sleep 0.5s - vibrate
    Vibrate.vibrateWithPauses(pauses);
    

    or for haptic feedback:

    // Choose from any of these available methods
    enum FeedbackType {
      success,
      error,
      warning,
      selection,
      impact,
      heavy,
      medium,
      light
    }
    
    var _type = FeedbackType.impact;
    Vibrate.feedback(_type);
    

    Source: https://github.com/clovisnicolas/flutter_vibrate

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-20 18:29

    Found a way using HapticFeedback.vibrate(). It works for both iOS and Android. See more details with code example and other settings on this [post][1]: Flutter: How to use HapticFeedback

    0 讨论(0)
提交回复
热议问题