当前位置:网站首页>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
边栏推荐
- C语言中sprintf()函数的用法
- Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
- 如何与博格华纳BorgWarner通过EDI传输业务数据?
- Exploration of mobile application performance tools
- Seal Library - installation and introduction
- DGraph: 大规模动态图数据集
- LeetCode 1. Sum of two numbers
- TCP congestion control details | 2 background
- Ap和F107数据来源及处理
- 【Leetcode】14. 最長公共前綴
猜你喜欢
Amazon cloud technology community builder application window opens
pwm呼吸燈
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
Ap和F107数据来源及处理
Lampe respiratoire PWM
伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
体验居家办公完成项目有感 | 社区征文
TCP congestion control details | 2 background
随机推荐
Penetration tool - intranet permission maintenance -cobalt strike
学习周刊-总第60期-2022年第25周
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
[error record] error -32000 received from application: there are no running service protocol
System Verilog implements priority arbiter
Usage of sprintf() function in C language
OpenHarmony如何启动远程设备的FA
How openharmony starts FA of remote devices
LeetCode 3. Longest substring without duplicate characters
System Verilog实现优先级仲裁器
Domestic relatively good OJ platform [easy to understand]
基于Impala的高性能数仓实践之执行引擎模块
Role and function of uboot
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
MOSFET器件手册关键参数解读
A few lines of code to complete RPC service registration and discovery
亚马逊云科技 Community Builder 申请窗口开启
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
剑指 Offer 25. 合并两个排序的链表