当前位置:网站首页>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命令行抓取到的各种回调事件的交互:
边栏推荐
- 微信小程序支付及退款整体流程
- 字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
- The grep command Shell regular expressions, the three musketeers
- proto3-2 syntax
- MySQL查询性能优化七种武器之索引潜水
- FFmpeg compiles and installs on win10 (configure libx264)
- WPF implements a MessageBox message prompt box with a mask
- 链表噩梦之一?5000多字带你弄清它的来龙去脉
- Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
- 微信支付开发流程
猜你喜欢

HAproxy: load balancing

【小程序】低代码+小游戏=小游戏可视化开发

太卷了... 腾讯一面被问到内存满了,会发生什么?

WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?

世界第4疯狂的科学家,在103岁生日那天去世了

京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点

基于CAP组件实现补偿事务与幂等性保障

数字化转型之支撑保障单元

微信一面:一致性哈希是什么,使用场景,解决了什么问题?

"Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
随机推荐
数据挖掘-05
Manchester city launch emotional intelligence scarf can be detected, give the fans
ABAP 报表中如何以二进制方式上传本地文件试读版
从零开始Blazor Server(9)--修改Layout
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
redis库没法引入
ansible-cmdb友好展示ansible收集主机信息
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
软件测试——金融测试类面试题,看完直接去面试了
proto3-2 syntax
goalng-sync/atomic原子操作
Scala 高阶(七):集合内容汇总(上篇)
WPF implements a MessageBox message prompt box with a mask
2022牛客多校(六)M. Z-Game on grid
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
LeetCode热题(11.合并两个有序链表)
手写大根堆
一甲子,正青春,CCF创建六十周年庆典在苏州举行
LeetCode #101. 对称二叉树
The grep command Shell regular expressions, the three musketeers