Flutter 学习 - Widget 之 手势识别

前言

在进行Android开发的时候我们通常会遇到事件这个词,比如OnTouch事件,点击事件等等,Android中的点击事件是直接在控件上进行添加,那在Flutter中我们需要怎么给Widget添加一些事件呢,这篇文章将介绍Flutter中的手势事件

在Flutter中我们发现按钮有onPressed来响应点击事件,但是我们如果想要监听Text,就会看到没有onPressed这个属性,想要给Text添加点击事件,就需要专门处理手势事件的Widget来嵌套Text,在Flutter中手势其实也是一个Widget,正是应了那么话,EveryThing is a Widget !!!

正文

Flutter 中 手势识别的Widget 是 GestureDetector

手势的使用很简单,我们只要在GestureDetector中嵌套需要检测手势的Widget就行,然后实现想要监听的手势的方法就行

效果图

老规矩,有图有真相,我们先看效果,
演示3.gif
tips:看右边控制台的输出

代码

下面是GestureDetector的代码示例

GestureDetector(
        child: Text(
          "点我",
          style: TextStyle(fontSize: 36),
        ),
        onDoubleTap: () {
          print("onDoubleTap");
        },
        onForcePressEnd: (ForcePressDetails forcePressDetails) {
        //   print("onForcePressEnd = " +
        //       forcePressDetails.localPosition.toString());
        },
        onHorizontalDragDown: (DragDownDetails dragDownDetails) {
        //   print("onHorizontalDragDown = " +
        //       dragDownDetails.localPosition.toString());
        },
        onLongPress: () {
          print("onLongPress");
        },
        onPanDown: (DragDownDetails dragDownDetails) {
        //   print("onPanDown = " + dragDownDetails.localPosition.toString());
        },
        onScaleStart: (ScaleStartDetails scaleStartDetails) {
        //   print("onScaleStart = " + scaleStartDetails.toString());
        },
        onSecondaryTapDown: (TapDownDetails tapDownDetails) {
        //   print("onSecondaryTapDown = " + tapDownDetails.toString());
        },
        onTap: () {
          print("onTap");
        },
        onVerticalDragDown: (DragDownDetails dragDownDetails) {
        //   print("onVerticalDragDown = " + dragDownDetails.toString());
        },
      )

源码分析

下面我们来看下GestureDetector的源码
注:参数均为可选

GestureDetector({
    Key key,
    this.child,
    this.onTapDown,
    this.onTapUp,
    this.onTap,
    this.onTapCancel,
    this.onSecondaryTapDown,
    this.onSecondaryTapUp,
    this.onSecondaryTapCancel,
    this.onDoubleTap,
    this.onLongPress,
    this.onLongPressStart,
    this.onLongPressMoveUpdate,
    this.onLongPressUp,
    this.onLongPressEnd,
    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,
    this.dragStartBehavior = DragStartBehavior.start,
  })
参数名字参数类型意义必选 or 可选
keyKeyWidget 的标识可选
childWidget要检测手势事件的 Widget可选
onTapDownGestureTapDownCallback手指触摸屏幕时产生 onTapDown 事件可选
onTapUpGestureTapDownCallback手指离开屏幕时产生 onTapUp 事件,之后便会触发 onTap 事件可选
onTapGestureTapCallback点击事件可选
onTapCancelGestureTapCancelCallback当触发 onTapDown 之后,取消点击,则会触发 onTapCancel,后面的 onTapDown 和 onTapUp 都不会在触发可选
onDoubleTapGestureTapCallback双击事件可选
onLongPressGestureLongPressCallback长按屏幕时触发,当监听了 onLongPress 事件时,则不能监听 onLongPressDragStart、onLongPressDragUpdate、onLongPressDragUp可选
onLongPressUpGestureLongPressUpCallback长按屏幕,手指离开屏幕时触发,当监听了 onLongPressUp 事件时,则不能监听 onLongPressDragStart、onLongPressDragUpdate、onLongPressDragUp可选
onLongPressDragStartGestureLongPressDragStartCallback长按屏幕,并准备开始拖动时触发,当监听了 onLongPressDragStart 事件时,则不能监听 onLongPress、onLongPressUp可选
onLongPressDragUpdateGestureLongPressDragUpdateCallback长按屏幕后并拖动时触发,当监听了 onLongPressDragUpdate 事件时,则不能监听 onLongPress、onLongPressUp可选
onLongPressDragUpGestureLongPressDragUpCallback长按屏幕拖动,手指离开屏幕时触发,当监听了 onLongPressDragUp 事件时,则不能监听 onLongPress、onLongPressUp可选
onVerticalDragDownGestureDragDownCallback手指接触屏幕,并且可能会开始垂直移动时触发可选
onVerticalDragStartGestureDragStartCallback手指接触屏幕,并且已经开始垂直移动时触发可选
onVerticalDragUpdateGestureDragUpdateCallback手指接触屏幕,并且垂直移动时触发可选
onVerticalDragEndGestureDragEndCallback手指接触屏幕垂直移动,然后手指离开屏幕时触发可选
onVerticalDragCancelGestureDragCancelCallback当 onVerticalDragDown 没有完成时就会触发 onVerticalDragCancel可选
onHorizontalDragDownGestureDragDownCallback手指接触屏幕,并且可能会开始水平移动时触发可选
onHorizontalDragStartGestureDragStartCallback手指接触屏幕,并且已经开始水平移动时触发可选
onHorizontalDragUpdateGestureDragUpdateCallback手指接触屏幕,并且水平移动时触发可选
onHorizontalDragEndGestureDragEndCallback手指接触屏幕水平移动,然后手指离开屏幕时触发可选
onHorizontalDragCancelGestureDragCancelCallback当 onHorizontalDragDown 没有完成时就会触发 onHorizontalDragCancel可选
onForcePressStartGestureForcePressStartCallback手指与屏幕接触,并且当有足够的压力时才会触发,注意,这个事件仅在具有压力检测屏幕的设备上触发。可选
onForcePressPeakGestureForcePressPeakCallback手指与屏幕接触,并且当有压力达到最大时触发,注意,这个事件仅在具有压力检测屏幕的设备上触发。可选
onForcePressUpdateGestureForcePressUpdateCallback手指与屏幕接触,有足够的压力并在屏幕上移动时触发,注意,这个事件仅在具有压力检测屏幕的设备上触发。可选
onForcePressEndGestureForcePressEndCallback手指与屏幕分开时触发,注意,这个事件仅在具有压力检测屏幕的设备上触发。可选
onPanDownGestureDragDownCallback手指与屏幕接触,并且可能开始移动时触发可选
onPanStartGestureDragStartCallback手指与屏幕接触,并且开始移动时触发可选
onPanUpdateGestureDragUpdateCallback手指在屏幕上移动时触发可选
onPanEndGestureDragEndCallback手指离开屏幕时触发可选
onPanCancelGestureDragCancelCallback当 onPanDown 没有完成时触发 onPanCancel可选
onScaleStartGestureTapDownCallback手指与屏幕接触,并且即将有缩放操作时触发,初始值为 1.0可选
onScaleUpdateGestureTapDownCallback手指与屏幕接触,并且有缩放操作时触发可选
onScaleEndGestureTapDownCallbackonScaleStart 之后,手指离开屏幕时触发可选
behaviorHitTestBehavior在命中测试期间,此手势检测器应如何表现。可选
excludeFromSemanticsbool是否从语义树中排除这些手势。因为 Widget可选
dragStartBehaviorDragStartBehavior确定处理拖动开始行为的方式。可选

关于Flutter中GestureDetector的使用方法就介绍完了。
以下是我的Flutter系列的链接,后续会持续更新,欢迎大家指正。

Flutter 系列文章

更多关于技术相关的内容请关注博主公众号–迷途程序猿
迷途程序猿

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值