当前位置:网站首页>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命令行抓取到的各种回调事件的交互:
边栏推荐
- web course design
- ABAP 报表中如何以二进制方式上传本地文件试读版
- 发明时代,「幂集创新」事关你我
- 腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
- Intranet penetration tool ngrok usage tutorial
- 十分钟教会你如何使用VitePress搭建及部署个人博客站点
- Adalvo acquires its first branded product, Onsolis
- 基于STM32+铂电阻设计的测温仪
- go基础之web获取参数
- 金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
猜你喜欢
随机推荐
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
The core key points of microservice architecture
手写大根堆
How should the acceptance criteria for R&D requirements be written?| Agile Practices
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
【微服务~远程调用】整合RestTemplate、WebClient、Feign
goalng-sync/atomic原子操作
Blazor Server (9) from scratch -- modify Layout
00后写个暑假作业,被监控成这笔样
Double pointer - the role of char **, int **
Golang学习之路(五):Golang的函数
electron 应用开发优秀实践
我们真的需要DApp吗?App真的不能满足我们的幻想吗?
合并两个有序列表
WeChat Mini Program Payment and Refund Overall Process
2022牛客多校(六)M. Z-Game on grid
荣耀携手Blue Yonder,加快企业战略增长
听声辨物,这是AI视觉该干的???|ECCV 2022
LeetCode #101. 对称二叉树
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端