Repository navigation
Expand file tree
/
Copy pathpositioned_tap_detector_2.dart
More file actions
233 lines (192 loc) · 5.89 KB
/
Copy pathpositioned_tap_detector_2.dart
File metadata and controls
233 lines (192 loc) · 5.89 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// //////////////////////////////////////////////////////////////////
// /// Based on the work by Ali Raghebi ///
// /// Now maintained here due to abandonment ///
// /// https://github.com/arsamme/flutter-positioned-tap-detector ///
// //////////////////////////////////////////////////////////////////
// ignore_for_file: public_member_api_docs
import 'dart:async';
import 'dart:math';
import 'package:flutter/widgets.dart';
typedef TapPositionCallback = void Function(TapPosition position);
@immutable
class PositionedTapDetector2 extends StatefulWidget {
const PositionedTapDetector2({
super.key,
this.child,
this.onTap,
this.onDoubleTap,
this.onSecondaryTap,
this.onLongPress,
Duration? doubleTapDelay,
this.behavior,
this.controller,
}) : doubleTapDelay = doubleTapDelay ?? _defaultDelay;
static const _defaultDelay = Duration(milliseconds: 250);
static const _doubleTapMaxOffset = 48.0;
final Widget? child;
final HitTestBehavior? behavior;
final TapPositionCallback? onTap;
final TapPositionCallback? onSecondaryTap;
final TapPositionCallback? onDoubleTap;
final TapPositionCallback? onLongPress;
final Duration doubleTapDelay;
final PositionedTapController? controller;
@override
State<PositionedTapDetector2> createState() => _TapPositionDetectorState();
}
class _TapPositionDetectorState extends State<PositionedTapDetector2> {
final _controller = StreamController<TapDownDetails>();
late final _stream = _controller.stream.asBroadcastStream();
Sink<TapDownDetails> get _sink => _controller.sink;
late StreamSubscription<TapDownDetails> _streamSub;
PositionedTapController? _tapController;
TapDownDetails? _pendingTap;
TapDownDetails? _firstTap;
@override
void initState() {
_updateController();
_startStream();
super.initState();
}
@override
void didUpdateWidget(PositionedTapDetector2 oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
_updateController();
}
if (widget.doubleTapDelay != oldWidget.doubleTapDelay) {
_streamSub.cancel().then(_startStream);
}
}
void _startStream([dynamic d]) {
_streamSub = _stream
.timeout(widget.doubleTapDelay)
.handleError(_onTimeout, test: (e) => e is TimeoutException)
.listen(_onTapConfirmed);
}
void _updateController() {
_tapController?._state = null;
if (widget.controller != null) {
widget.controller!._state = this;
_tapController = widget.controller;
}
}
void _onTimeout(dynamic error) {
final firstTap = _firstTap;
if (firstTap != null && _pendingTap == null) {
_postCallback(firstTap, widget.onTap);
}
}
void _onTapConfirmed(TapDownDetails details) {
if (_firstTap == null) {
_firstTap = details;
} else {
_handleSecondTap(details);
}
}
void _handleSecondTap(TapDownDetails secondTap) {
final firstTap = _firstTap;
if (firstTap == null) return;
if (_isDoubleTap(firstTap, secondTap)) {
_postCallback(secondTap, widget.onDoubleTap);
} else {
_postCallback(firstTap, widget.onTap);
_postCallback(secondTap, widget.onTap);
}
}
bool _isDoubleTap(TapDownDetails d1, TapDownDetails d2) {
final dx = d1.globalPosition.dx - d2.globalPosition.dx;
final dy = d1.globalPosition.dy - d2.globalPosition.dy;
return sqrt(dx * dx + dy * dy) <=
PositionedTapDetector2._doubleTapMaxOffset;
}
void _onTapDownEvent(TapDownDetails details) {
_pendingTap = details;
}
void _onTapEvent() {
final pending = _pendingTap;
if (pending == null) return;
if (widget.onDoubleTap == null) {
_postCallback(pending, widget.onTap);
} else {
_sink.add(pending);
}
_pendingTap = null;
}
void _onSecondaryTapEvent() {
final pending = _pendingTap;
if (pending == null) return;
_postCallback(pending, widget.onSecondaryTap);
_pendingTap = null;
}
void _onLongPressEvent() {
final pending = _pendingTap;
if (pending != null) {
if (_firstTap == null) {
_postCallback(pending, widget.onLongPress);
} else {
_sink.add(pending);
_pendingTap = null;
}
}
}
Future<void> _postCallback(
TapDownDetails details,
TapPositionCallback? callback,
) async {
_firstTap = null;
if (callback != null) {
callback(TapPosition(details.globalPosition, details.localPosition));
}
}
@override
void dispose() {
_controller.close();
_streamSub.cancel();
_tapController?._state = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
if (widget.controller != null) {
if (widget.child != null) {
return widget.child!;
} else {
return Container();
}
}
return GestureDetector(
behavior: widget.behavior ??
(widget.child == null
? HitTestBehavior.translucent
: HitTestBehavior.deferToChild),
onTap: _onTapEvent,
onLongPress: _onLongPressEvent,
onTapDown: _onTapDownEvent,
onSecondaryTapDown: _onTapDownEvent,
onSecondaryTap: _onSecondaryTapEvent,
child: widget.child,
);
}
}
class PositionedTapController {
_TapPositionDetectorState? _state;
void onTap() => _state?._onTapEvent();
void onSecondaryTap() => _state?._onSecondaryTapEvent();
void onLongPress() => _state?._onLongPressEvent();
void onTapDown(TapDownDetails details) => _state?._onTapDownEvent(details);
}
@immutable
class TapPosition {
const TapPosition(this.global, this.relative);
final Offset global;
final Offset? relative;
@override
bool operator ==(Object other) {
if (other is! TapPosition) return false;
final typedOther = other;
return global == typedOther.global && relative == other.relative;
}
@override
int get hashCode => Object.hash(global, relative);
}