当前位置:网站首页>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
边栏推荐
- Usage of sprintf() function in C language
- DGraph: 大规模动态图数据集
- Use of openpose
- QWebEngineView崩溃及替代方案
- 七张图,学会做有价值的经营分析
- Role and function of uboot
- 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
- linux安装postgresql + patroni 集群问题
- Easy language ABCD sort
- 基于Impala的高性能数仓实践之执行引擎模块
猜你喜欢

福元医药上交所上市:市值105亿 胡柏藩身价超40亿
![[essay solicitation activity] Dear developer, RT thread community calls you to contribute](/img/31/11409606718e0f4837f4cc572172a3.png)
[essay solicitation activity] Dear developer, RT thread community calls you to contribute

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

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

PhD battle-11 preview | review and prospect backdoor attack and defense of neural network

【Leetcode】13. Roman numeral to integer

Exploration and practice of integration of streaming and wholesale in jd.com

Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
![[leetcode] 14. Préfixe public le plus long](/img/70/e5be1a7c2e10776a040bfc8d7711a0.png)
[leetcode] 14. Préfixe public le plus long

Geoserver: publishing PostGIS data sources
随机推荐
【Leetcode】14. 最長公共前綴
2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
What is the difference between JSP and servlet?
人生的开始
【Leetcode】14. 最长公共前缀
七张图,学会做有价值的经营分析
Domestic relatively good OJ platform [easy to understand]
国内比较好的OJ平台[通俗易懂]
博客主题 “Text“ 夏日清新特别版
Geoserver: publishing PostGIS data sources
System Verilog实现优先级仲裁器
什么是泛型?- 泛型入门篇
Detailed explanation of @accessories annotation of Lombok plug-in
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
871. 最低加油次数
Ap和F107数据来源及处理
Day 18 of leetcode dynamic planning introduction
Role and function of uboot
How openharmony starts FA of remote devices
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP