如何在 Android 和 iOS 上使用 Flutter 实现振动?

Mih*_*iha 13 vibration dart flutter

使用 Flutter 我试图在单击按钮时实现振动。

我发现说实话很难。我尝试使用以下软件包失败: 振动振动,但它们只是不振动(这里谈论的是真实设备而不是模拟器/模拟器)。

是否有关于如何使用 Flutter 在 iOS 和 Android 设备上实现振动的清晰示例指南?

谢谢,米海

Ily*_*ent 22

Flutter 2021 最简单的解决方案

1)导入标准服务:

import 'package:flutter/services.dart';
Run Code Online (Sandbox Code Playgroud)

2)用途:

HapticFeedback.mediumImpact();
Run Code Online (Sandbox Code Playgroud)

lightImpact, 或heavyImpact

3) 不要忘记在 Android Manifest 中添加以下内容:

<uses-permission android:name="android.permission.VIBRATE"/>
Run Code Online (Sandbox Code Playgroud)

奖励)播放“咔哒”声:

SystemSound.play(SystemSoundType.click);
Run Code Online (Sandbox Code Playgroud)


Mih*_*iha 15

找到了一种使用HapticFeedback.vibrate(). 它适用于 iOS 和 Android。查看此 [post][1] 上的代码示例和其他设置的更多详细信息:Flutter:如何使用 HapticFeedback

  • 我创建了一个使用 Flutter 实现振动的快速教程,以防您遇到困难。看一下:https://medium.com/tagmalogic/implement-vibration-with-flutter-c0a7d2d4007?source=friends_link&amp;sk=6976429f156a8a7095e939f85be62f3e (2认同)

Cas*_*rin 7

我建议使用flutter_vibrate,它适用于 ios/android。设置更简单并且效果很好。

  1. 只需将 flutter_vibrate 添加为 pubspec.yaml 文件中的依赖项即可。

    依赖项:振动:^1.3.0

  2. 确保将以下权限添加到您的 Android 清单中

  3. 导入包:

    导入'包:振动/振动.dart';

例子:

默认振动500ms:

Vibration.vibrate();
       
Run Code Online (Sandbox Code Playgroud)

振动1000ms:

Vibration.vibrate(duration: 1000);
           
Run Code Online (Sandbox Code Playgroud)

模式:等待0.5秒,振动1秒,等待0.5秒,振动2秒,等待0.5秒,振动3秒,等待0.5秒,振动0.5秒:

Vibration.vibrate(pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500]);
            
Run Code Online (Sandbox Code Playgroud)

模式:等待0.5秒,振动1秒,等待0.5秒,振动2秒,等待0.5秒,振动3秒,等待0.5秒,振动0.5秒':

Vibration.vibrate( pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500], intensities: [128, 255, 64, 255]);
Run Code Online (Sandbox Code Playgroud)

安卓

AndroidManifest.xml 中需要 VIBRATE 权限。

支持振动的持续时间和模式。在 Android 8 (Oreo) 及更高版本上,使用 [VibrationEffect][1] 类。有关其余使用说明,请参阅 [Vibrator][1] 类文档。

iOS系统

支持 CoreHaptics 设备上的振动持续时间和模式。在较旧的设备上,该模式是通过 500 毫秒长的振动来模拟的。您可以使用hasCustomVibrationsSupport检查当前设备是否支持 CoreHaptics 。

  • 我已经尝试过这个包了。它抛出太多运行时错误 (2认同)

laa*_*IGL 5

首先,您需要将其添加vibrate:pubspec.yaml文件的依赖项。

dependencies:
  flutter:
    sdk: flutter
  vibrate:
Run Code Online (Sandbox Code Playgroud)

之后,您需要将包导入到您正在使用的类中。

// Import package
import 'package:vibrate/vibrate.dart';
Run Code Online (Sandbox Code Playgroud)

现在你可以这样进行振动

// 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);
Run Code Online (Sandbox Code Playgroud)

或用于触觉反馈

// Choose from any of these available methods
enum FeedbackType {
  success,
  error,
  warning,
  selection,
  impact,
  heavy,
  medium,
  light
}

var _type = FeedbackType.impact;
Vibrate.feedback(_type);
Run Code Online (Sandbox Code Playgroud)

来源: https: //github.com/clovisnicolas/flutter_vibrate