当前位置:网站首页>Flutter: 动作反馈
Flutter: 动作反馈
2022-07-02 14:33:00 【J_D_Chi】
写在前面
在 Android 或 iOS 设备上,系统会对提供声音或是震动的反馈来帮助我们让用户在进行一些操作时,起到一个告知用户这个操作是有效的作用。本篇主要是总结在 Flutter 上,如何使用这些功能。
内容
Feedback
一般我们使用 GestureDetector都会用到它的 onTap或 onLongTap方法,此时我们可以借助 FeedBack类来帮助我们添加特定平台的反馈。
class WidgetWithWrappedHandler extends StatelessWidget {
const WidgetWithWrappedHandler({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: Feedback.wrapForTap(_onTapHandler, context),
onLongPress: Feedback.wrapForLongPress(_onLongPressHandler, context),
child: const Text('X'),
);
}
void _onTapHandler() {
// Respond to tap.
}
void _onLongPressHandler() {
// Respond to long press.
}
}
根据目前官方文档里 Feedback方法的介绍,在 Android 上,点按操作会播放点击系统声音,长按的话会有震动反馈。而在 iOS 则不会有反馈,因为 iOS 里通常对这两个操作不会有响应。
Feedback还提供了另外两个方法forTap和forLongPress,以进一步帮助我们在反馈上做多一些其它的工作,例如判断这个操作是否有一些其它条件具备了,才去执行反馈。
class WidgetWithExplicitCall extends StatelessWidget {
const WidgetWithExplicitCall({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
// Do some work (e.g. check if the tap is valid)
Feedback.forTap(context);
// Do more work (e.g. respond to the tap)
},
onLongPress: () {
// Do some work (e.g. check if the long press is valid)
Feedback.forLongPress(context);
// Do more work (e.g. respond to the long press)
},
child: const Text('X'),
);
}
}
HapticFeedback
在 Flutter 的 services 包里,有一个 HapticFeedback类,里面提供了几个静态方法,用于提供震动反馈:
class HapticFeedback {
static Future<void> vibrate();
static Future<void> lightImpact();
static Future<void> mediumImpact();
static Future<void> heavyImpact();
static Future<void> selectionClick();
}
这些方法也是 Flutter 的一些官方组件里所用到的,例如下拉刷新,时间选择器里调节时间等。
参考
Feedback class
HapticFeedback class
【Flutter 小知识】震动反馈 HapticFeedback
边栏推荐
- 远程办公对我们的各方面影响心得 | 社区征文
- [leetcode] 14. Préfixe public le plus long
- 国内比较好的OJ平台[通俗易懂]
- 【Leetcode】14. 最长公共前缀
- Talk about an experience of job hopping and being rejected
- P6774 [NOI2020] 时代的眼泪(分块)
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- 酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
- 【Leetcode】14. Longest Common Prefix
- C语言自定义函数的方法
猜你喜欢

QStyle实现自绘界面项目实战(二)

A case study of college entrance examination prediction based on multivariate time series

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

移动应用性能工具探索之路

Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
![[leetcode] 14. Préfixe public le plus long](/img/70/e5be1a7c2e10776a040bfc8d7711a0.png)
[leetcode] 14. Préfixe public le plus long

剑指 Offer 24. 反转链表

畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权

剑指 Offer 27. 二叉树的镜像

13、Darknet YOLO3
随机推荐
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
LeetCode 5. Longest Palindromic Substring
Seal Library - installation and introduction
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
远程办公对我们的各方面影响心得 | 社区征文
Executive engine module of high performance data warehouse practice based on Impala
PWM breathing lamp
What will you do after digital IC Verification?
Exploration of mobile application performance tools
Tech talk activity preview | building intelligent visual products based on Amazon kVs
How to transfer business data with BorgWarner through EDI?
Usage of sprintf() function in C language
871. 最低加油次数
深度之眼(二)——矩阵及其基本运算
MySQL port
Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era
Digital IC hand tearing code -- voting device
C语言中sprintf()函数的用法
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])