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_vibrate,它适用于 ios/android。设置更简单并且效果很好。
只需将 flutter_vibrate 添加为 pubspec.yaml 文件中的依赖项即可。
依赖项:振动:^1.3.0
确保将以下权限添加到您的 Android 清单中
导入包:
导入'包:振动/振动.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 。
首先,您需要将其添加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
归档时间: |
|
查看次数: |
14421 次 |
最近记录: |