一雏逾、GestureDetector 的介紹
GestureDetector:是手勢(shì)識(shí)別的組件,可以識(shí)別點(diǎn)擊屑宠、雙擊仇让、長(zhǎng)按事件、拖動(dòng)卫玖、縮放等手勢(shì)
二踊淳、GestureDetector的源碼介紹
GestureDetector({
Key key,
this.child,
this.onTapDown,//按下時(shí)回調(diào)
this.onTapUp,//抬起時(shí)回調(diào)
this.onTap,//點(diǎn)擊事件回調(diào)
this.onTapCancel,//點(diǎn)擊取消事件回調(diào)
this.onSecondaryTap,
this.onSecondaryTapDown,
this.onSecondaryTapUp,
this.onSecondaryTapCancel,
this.onTertiaryTapDown,
this.onTertiaryTapUp,
this.onTertiaryTapCancel,
this.onDoubleTapDown,//短時(shí)間內(nèi)雙擊按下時(shí)回調(diào)
this.onDoubleTap,//短時(shí)間內(nèi)雙擊回調(diào)
this.onDoubleTapCancel,//短時(shí)間內(nèi)雙擊取消事件回調(diào)
this.onLongPress,//長(zhǎng)按事件回調(diào)
this.onLongPressStart,//長(zhǎng)按開(kāi)始事件回調(diào)
this.onLongPressMoveUpdate,//長(zhǎng)按移動(dòng)事件回調(diào)
this.onLongPressUp,//長(zhǎng)按抬起事件回調(diào)
this.onLongPressEnd,//長(zhǎng)按結(jié)束事件回調(diào)
this.onSecondaryLongPress,
this.onSecondaryLongPressStart,
this.onSecondaryLongPressMoveUpdate,
this.onSecondaryLongPressUp,
this.onSecondaryLongPressEnd,
this.onVerticalDragDown,//垂直滑動(dòng)按下事件回調(diào)
this.onVerticalDragStart,//垂直滑動(dòng)開(kāi)始事件回調(diào)
this.onVerticalDragUpdate,//垂直滑動(dòng)更新事件回調(diào)
this.onVerticalDragEnd,//垂直滑動(dòng)結(jié)束事件回調(diào)
this.onVerticalDragCancel,//垂直滑動(dòng)取消事件回調(diào)
this.onHorizontalDragDown,//水平滑動(dòng)按下事件回調(diào)
this.onHorizontalDragStart,///水平滑動(dòng)開(kāi)始事件回調(diào)
this.onHorizontalDragUpdate,//水平滑動(dòng)更新事件回調(diào)
this.onHorizontalDragEnd,//水平滑動(dòng)結(jié)束事件回調(diào)
this.onHorizontalDragCancel,//水平滑動(dòng)取消事件回調(diào)
this.onForcePressStart,//回調(diào)函數(shù)只會(huì)在有壓力的設(shè)備上被觸發(fā)
this.onForcePressPeak,
this.onForcePressUpdate,
this.onForcePressEnd,
this.onPanDown,//按壓時(shí)回調(diào)
this.onPanStart,//按壓拖動(dòng)開(kāi)始回調(diào)
this.onPanUpdate,//按壓拖動(dòng)回調(diào)
this.onPanEnd,//按壓拖動(dòng)結(jié)束回調(diào)
this.onPanCancel,//按壓拖動(dòng)取消回調(diào)
this.onScaleStart,//縮放開(kāi)始事件回調(diào)
this.onScaleUpdate,//縮放更新事件回調(diào)
this.onScaleEnd,//縮放結(jié)束事件回調(diào)
this.behavior,//手勢(shì)檢測(cè)器在觸摸中應(yīng)該如何工作
this.excludeFromSemantics = false,
this.dragStartBehavior = DragStartBehavior.start,
}) : assert(excludeFromSemantics != null),
assert(dragStartBehavior != null),
assert(() {
final bool haveVerticalDrag = onVerticalDragStart != null || onVerticalDragUpdate != null || onVerticalDragEnd != null;
final bool haveHorizontalDrag = onHorizontalDragStart != null || onHorizontalDragUpdate != null || onHorizontalDragEnd != null;
final bool havePan = onPanStart != null || onPanUpdate != null || onPanEnd != null;
final bool haveScale = onScaleStart != null || onScaleUpdate != null || onScaleEnd != null;
if (havePan || haveScale) {
if (havePan && haveScale) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('Incorrect GestureDetector arguments.'),
ErrorDescription(
'Having both a pan gesture recognizer and a scale gesture recognizer is redundant; scale is a superset of pan.'
),
ErrorHint('Just use the scale gesture recognizer.')
]);
}
final String recognizer = havePan ? 'pan' : 'scale';
if (haveVerticalDrag && haveHorizontalDrag) {
throw FlutterError(
'Incorrect GestureDetector arguments.\n'
'Simultaneously having a vertical drag gesture recognizer, a horizontal drag gesture recognizer, and a $recognizer gesture recognizer '
'will result in the $recognizer gesture recognizer being ignored, since the other two will catch all drags.'
);
}
}
return true;
}()),
super(key: key);
三、GestureDetector的屬性介紹
屬性 | 說(shuō)明 |
---|---|
onTapDown | 按下時(shí)回調(diào) |
onTapUp | 抬起時(shí)回調(diào) |
onTap | 點(diǎn)擊事件回調(diào) |
onTapCancel | 點(diǎn)擊取消事件回調(diào) |
onDoubleTapDown | 短時(shí)間內(nèi)雙擊按下時(shí)回調(diào) |
onDoubleTap | 短時(shí)間內(nèi)雙擊回調(diào) |
onDoubleTapCancel | 短時(shí)間內(nèi)雙擊取消事件回調(diào) |
onLongPress | 長(zhǎng)按事件回調(diào) |
onLongPressStart | 長(zhǎng)按開(kāi)始事件回調(diào) |
onLongPressMoveUpdate | 長(zhǎng)按移動(dòng)事件回調(diào) |
onLongPressUp | 長(zhǎng)按抬起事件回調(diào) |
onLongPressEnd | 長(zhǎng)按結(jié)束事件回調(diào) |
onVerticalDragDown | 垂直滑動(dòng)按下事件回調(diào) |
onVerticalDragStart | 垂直滑動(dòng)開(kāi)始事件回調(diào) |
onVerticalDragUpdate | 垂直滑動(dòng)更新事件回調(diào) |
onVerticalDragEnd | 垂直滑動(dòng)結(jié)束事件回調(diào) |
onVerticalDragCancel | 垂直滑動(dòng)取消事件回調(diào) |
onHorizontalDragDown | 水平滑動(dòng)按下事件回調(diào) |
onHorizontalDragStart | 水平滑動(dòng)開(kāi)始事件回調(diào) |
onHorizontalDragUpdate | 水平滑動(dòng)更新事件回調(diào) |
onHorizontalDragEnd | 水平滑動(dòng)結(jié)束事件回調(diào) |
onHorizontalDragCancel | 水平滑動(dòng)取消事件回調(diào) |
onPanDown | 按壓時(shí)回調(diào)(我自己理解為點(diǎn)擊事件芦劣,但是比onTap優(yōu)先級(jí)高说榆,onPan先執(zhí)行先取消,onPan比onTap多個(gè)滑動(dòng)監(jiān)聽(tīng)) |
onPanStart | 按壓拖動(dòng)開(kāi)始回調(diào) 按壓開(kāi)始初肉,不能與 onScale ,onVerticalDrag碧磅,onHorizontalDrag杈抢,同時(shí)使用 |
onPanUpdate | 按壓拖動(dòng)回調(diào) |
onPanEnd | 按壓拖動(dòng)結(jié)束回調(diào) |
onPanCancel | 按壓拖動(dòng)取消回調(diào) |
onScaleStart | 縮放開(kāi)始事件回調(diào) 縮放開(kāi)始庆捺,不能與 onPan ,onVerticalDrag捉腥,onHorizontalDrag你画,同時(shí)使用 |
onScaleUpdate | 縮放更新事件回調(diào) |
onScaleEnd | 縮放結(jié)束事件回調(diào) |
behavior | 手勢(shì)檢測(cè)器在觸摸中應(yīng)該如何工作 HitTestBehavior.deferToChild:只有當(dāng)前容器中的child被點(diǎn)擊時(shí)才會(huì)響應(yīng)點(diǎn)擊事件 HitTestBehavior.opaque:點(diǎn)擊整個(gè)區(qū)域都會(huì)響應(yīng)點(diǎn)擊事件,但是點(diǎn)擊事件不可穿透向下傳遞立磁,注釋翻譯:阻止視覺(jué)上位于其后方的目標(biāo)接收事件剥槐。 HitTestBehavior.translucent:同樣是點(diǎn)擊整個(gè)區(qū)域都會(huì)響應(yīng)點(diǎn)擊事件宪摧,和opaque的區(qū)別是點(diǎn)擊事件是否可以向下傳遞,注釋翻譯:半透明目標(biāo)既可以在其范圍內(nèi)接受事件蕊苗,也可以允許視覺(jué)上位于其后方的目標(biāo)接收事件 |
四沿彭、一些觸摸事件,監(jiān)聽(tīng)的調(diào)用順序
4.1瞧柔、點(diǎn)擊屏幕:onPanDown--onPanCancel--onTapDown--onTapUp--onTap
4.2睦裳、點(diǎn)擊滑動(dòng):onPanDown--onTapDown--onTapCancel--onPanStart--onPanUpdate--onPanEnd
4.3、雙擊屏幕:onPanDown--onPanCancel--onDoubleTapDown--onPanDown--onPanCancel--onDoubleTap
4.4哥蔚、長(zhǎng)按屏幕:onPanDown--onTapDown--onTapCancel--onPanCancel--onLongPressStart--onLongPress--onLongPressMoveUpdate--onLongPressEnd--onLongPressUp
4.5、手指左右滑動(dòng):onVerticalDragDown--onHorizontalDragDown--onVerticalDragCancel--onHorizontalDragStart--onHorizontalDragUpdate--onHorizontalDragEnd
4.6糙箍、手指上下滑動(dòng):onVerticalDragDown--onHorizontalDragDown--onHorizontalDragCancel--onVerticalDragStart--onVerticalDragUpdate--onVerticalDragEnd
4.7、縮放:onScaleStart--onScaleUpdate--onScaleEnd
五深夯、GestureDetector的demo
5.1塌西、給組件添加按鍵監(jiān)聽(tīng)
class _GestureDetectorFulState extends State<GestureDetectorFul> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("GestureDetector學(xué)習(xí)"),
),
body: Center(
child: GestureDetector(
//一定要加(按鈕和文字之間的空白,也添加監(jiān)聽(tīng)了)
behavior: HitTestBehavior.opaque,
onPanDown: (value) {
print("我是onPanDown");
},
child: Row(
children: [
OutlinedButton(
child: Text("按鈕"),
onPressed: () {
print("點(diǎn)擊了button");
},
),
SizedBox(width: 80),
Text("文字")
],
)
),
)
));
}
}