Flutter手勢處理
在Flutter中的手勢事件分為兩層。
第一層有原始指針事件,它描述了屏幕上指針(例如,觸摸,鼠標和觸控筆)的位置和移動。
第二層有手勢,描述由一個或多個指針移動組成的語義動作。
在flutter中簡單的手勢處理用封裝好的GestureDetector來處理就好了。
GestureDetector中為我們提供的方法有:
onTapDown:按下
onTap:點擊動作
onTapUp:抬起
onTapCancel:觸發(fā)了 onTapDown,但并沒有完成一個 onTap 動作
onDoubleTap:雙擊
onLongPress:長按
onScaleStart, onScaleUpdate, onScaleEnd:縮放
onVerticalDragDown, onVerticalDragStart, onVerticalDragUpdate, onVerticalDragEnd, onVerticalDragCancel, onVerticalDragUpdate:在豎直方向上移動
onHorizontalDragDown, onHorizontalDragStart, onHorizontalDragUpdate, onHorizontalDragEnd, onHorizontalDragCancel, onHorizontalDragUpdate:在水平方向上移動
onPanDown, onPanStart, onPanUpdate, onPanEnd, onPanCancel:拖曳(觸碰到屏幕、在屏幕上移動)
以onTap、onDoubleTap、onLongPress為例rt:

image
附上代碼:
//注意,這里的 Fluttertoast 需要導(dǎo)包
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: GestureDetector(
onTap: (){
Fluttertoast.showToast(msg: "你點擊了一次",
toastLength: Toast.LENGTH_SHORT);
},
onDoubleTap: (){
Fluttertoast.showToast(msg: "你雙擊了一次",
toastLength: Toast.LENGTH_SHORT);
},
onLongPress: (){
Fluttertoast.showToast(msg: "你長按了一次",
toastLength: Toast.LENGTH_SHORT);
},
child: Container(
child: Image.asset("images/nyy.png"),
),
),
);
}
導(dǎo)包:
dev_dependencies:
fluttertoast: ^3.1.1
大功告成!其他的方法讀者可自行嘗試。