手势检测(点击、双击、长按)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import 'package:flutter/material.dart';

class GestureDetectorTestRoute extends StatefulWidget {
@override
_GestureDetectorTestRouteState createState() =>
new _GestureDetectorTestRouteState();
}

class _GestureDetectorTestRouteState extends State<GestureDetectorTestRoute> {
String _operation = "No Gesture detected!"; //保存事件名
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("手势检测(点击、双击、长按)"),
),
body: Container(
child: GestureDetector(
child: Container(
alignment: Alignment.center,
color: Colors.blue,
width: 200.0,
height: 100.0,
child: Text(
_operation,
style: TextStyle(color: Colors.white),
),
),
onTap: () => updateText("Tap点击"), //点击
onDoubleTap: () => updateText("DoubleTap双击"), //双击
onLongPress: () => updateText("LongPress长按"), //长按
),
),
);
}

void updateText(String text) {
//更新显示的事件名
setState(() {
_operation = text;
});
}
}