android - 长按时 flutter 触觉反馈

我们应该如何使用 Flutter HapticFeedback 类在长按时产生触觉反馈?

我目前正在使用 OnTapDown 方法期间使用 HapticFeedback.selectionClick(),但没有任何反应。

我也已经在 android list 文件中添加了振动权限。我正在使用 Pixel2 XL 设备进行测试。

最佳答案

你可以使用 vibrate 插件。
在您的 pubspec.yaml 文件中添加此依赖项

vibrate: ^0.0.4  

在你的类里面使用这个函数来振动

void vibrate() async {
    bool canVibrate = await Vibrate.canVibrate;
    canVibrate ? Vibrate.feedback(FeedbackType.medium) : null;
  }

onLongPress 事件被触发时,从您的小部件中调用上述函数

 onLongPress: () {
          vibrate();
        },

More about vibrate plugin.
希望对您有所帮助。

https://stackoverflow.com/questions/54130718/

相关文章:

http - flutter http header

android - 错误 : The class 'SingleTickerProviderStat

dart - 在 Flutter 中,在 pubspec.yaml 文件中添加包的最佳方法是什么?

flutter - 在 flutter 中将数据作为对象存储在共享首选项中

firebase - 如何在 Flutter 中用 Column 或 ListView 类包装 St

dart - Flutter:主题未应用于文本小部件

flutter - Flutter 中的 initState 和 super.initState 是

firebase - flutter firestore,在数组中添加新对象

android - 在 Flutter 的登录屏幕中显示循环进度对话框,如何在 Flutter 中实

flutter - 使用 FutureBuilder 时重新加载数据