当前位置:网站首页>Flutter入门进阶之旅(七)GestureDetector
Flutter入门进阶之旅(七)GestureDetector
2022-08-09 12:04:00 【谢栋_】
引言:
GestureDetector在Flutter中负责处理跟用户的简单手势交互,GestureDetector控件没有图像展示,只是检测用户输入的手势,并作出相应的处理,包括点击、拖动和缩放。许多控件使用GestureDetector为其他控件提供回调,比如IconButton、RaisedButton和FloatingActionButton控件有onPressed回调,当用户点击控件时触发回调,当用户点击控件时触发回调。
我们来一起看下GestureDetector的构造方法:
GestureDetector({
Key key,
this.child,
this.onTapDown,
this.onTapUp,
this.onTap,
this.onTapCancel,
this.onDoubleTap,
this.onLongPress,
this.onLongPressUp,
this.onVerticalDragDown,
this.onVerticalDragStart,
this.onVerticalDragUpdate,
this.onVerticalDragEnd,
this.onVerticalDragCancel,
this.onHorizontalDragDown,
this.onHorizontalDragStart,
this.onHorizontalDragUpdate,
this.onHorizontalDragEnd,
this.onHorizontalDragCancel,
this.onForcePressStart,
this.onForcePressPeak,
this.onForcePressUpdate,
this.onForcePressEnd,
this.onPanDown,
this.onPanStart,
this.onPanUpdate,
this.onPanEnd,
this.onPanCancel,
this.onScaleStart,
this.onScaleUpdate,
this.onScaleEnd,
this.behavior,
this.excludeFromSemantics = false
})
从构造方法中,我们看出GestureDetector构造方法里定义各种事件回调,还有一个child属性,这就意味着我们可以利用GestureDetector包裹本身不支持点击回调事件的Widget赋予它们点击回调能力,像Text、Image我们就不能像使用RaisedButton一样直接给Text、Image绑定onPress回调,但是我们可以借助GestureDetector完成这一操作。
如图我给Text赋予了各种事件交互:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(home: new MyApp()));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Gestures"),
),
body: new Center(
child: new GestureDetector(
child: new Text("我被赋予了点击触摸能力...",style: new TextStyle(fontSize: 20.0),),
onTap: () {
print("------onTap");
},
onDoubleTap: () {
print("------onDoubleTap");
},
onLongPress: () {
print("-----onLongPress");
},
onVerticalDragStart: (details){
print("在垂直方向开始位置:"+details.globalPosition.toString());
}, onVerticalDragEnd: (details){
print("在垂直方向结束位置:"+details.primaryVelocity.toString());
},
)),
);
}
}
我们在log命令行抓取到的各种回调事件的交互:
边栏推荐
- The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
- "Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
- LeetCode热题(11.合并两个有序链表)
- 又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
- K个结点的组内逆序调整
- 听声辨物,这是AI视觉该干的???|ECCV 2022
- 1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
- 微信小程序支付及退款整体流程
- Recommend a free 50-hour AI computing platform
- 链表噩梦之一?5000多字带你弄清它的来龙去脉
猜你喜欢
Scala 高阶(七):集合内容汇总(上篇)
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
研发需求的验收标准应该怎么写? | 敏捷实践
h264协议
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
告别手摇织布机的AI时代
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
AQS Synchronization Component - FutureTask Analysis and Use Cases
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
报告:想学AI的学生数量已涨200%,老师都不够用了
随机推荐
Nature:猪死亡1小时后,器官再次运转
基于STM32+铂电阻设计的测温仪
K个结点的组内逆序调整
微信小程序支付及退款整体流程
基于CAP组件实现补偿事务与幂等性保障
【HCIP持续更新】IS-IS协议原理与配置
electron 应用开发优秀实践
【无标题】
LeetCode热题(11.合并两个有序链表)
十分钟教会你如何使用VitePress搭建及部署个人博客站点
MongoDB-查询中$all的用法介绍
脱光衣服待着就能减肥,当真有这好事?
Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot
Go-based web access parameters
Web console control edit box
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
李开复花上千万投的缝纫机器人,团队出自大疆
The core key points of microservice architecture
数据挖掘-06
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...