当前位置:网站首页>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
边栏推荐
猜你喜欢
上传代码到远程仓库报错error: remote origin already exists.
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
Connect Porsche and 3PL EDI cases
Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
Leetcode1380: lucky numbers in matrix
QWebEngineView崩溃及替代方案
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
A few lines of code to complete RPC service registration and discovery
Believe in yourself and finish the JVM interview this time
随机推荐
13、Darknet YOLO3
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
OpenPose的使用
使用知行之桥的API端口,提供资源供合作伙伴访问
[error record] error -32000 received from application: there are no running service protocol
一年顶十年
什么是泛型?- 泛型入门篇
对接保时捷及3PL EDI案例
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
【Leetcode】13. 罗马数字转整数
Learning Weekly - total issue 60 - 25th week of 2022
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
Geoserver: publishing PostGIS data sources
Leetcode1380: lucky numbers in matrix
[cloud native] briefly talk about the understanding of flume, a massive data collection component
LeetCode 1. Sum of two numbers
Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
LeetCode 3. Longest substring without duplicate characters
IP地址转换地址段
LeetCode 6. Zigzag transformation (n-shaped transformation)