import 'package:flutter/gestures.dart'; import 'package:flutter/widgets.dart'; /// Taps on the peek open that chat; drags still reach its transcript. /// /// Placed above the peek's content, the recognizer joins every pointer's arena /// along with the transcript's own recognizers. A move past the touch slop /// rejects it, so the scrollable takes the drag as usual. A release without /// one accepts it at once, ahead of the sweep that would otherwise hand the /// tap to a button or link under the finger: a tap anywhere on the peek /// activates the chat and nothing inside it is pressed. class ChatPeekTapRecognizer extends OneSequenceGestureRecognizer { ChatPeekTapRecognizer({super.debugOwner, super.supportedDevices}); VoidCallback? onTap; Offset? _origin; bool _won = false; bool _released = false; @override void addAllowedPointer(PointerDownEvent event) { startTrackingPointer(event.pointer, event.transform); _origin = event.position; _won = false; _released = false; } @override void handleEvent(PointerEvent event) { if (event is PointerMoveEvent) { final slop = computeHitSlop(event.kind, gestureSettings); if ((event.position - _origin!).distance > slop) { resolve(GestureDisposition.rejected); stopTrackingPointer(event.pointer); } } else if (event is PointerUpEvent) { _released = true; if (_won) { _tap(); } else { // Alone or not, the win must come before the arena's sweep. resolve(GestureDisposition.accepted); } stopTrackingPointer(event.pointer); } else if (event is PointerCancelEvent) { resolve(GestureDisposition.rejected); stopTrackingPointer(event.pointer); } } @override void acceptGesture(int pointer) { // As the arena's only member the win arrives on the press; the tap still // waits for the release, and a drag past the slop cancels it. if (_released) { _tap(); } else { _won = true; } } @override void rejectGesture(int pointer) => stopTrackingPointer(pointer); @override void didStopTrackingLastPointer(int pointer) { _origin = null; _won = false; _released = false; } void _tap() { if (onTap case final tap?) invokeCallback('onTap', tap); } @override String get debugDescription => 'chat peek tap'; } /// Lets [child] (the peek's content) scroll under a drag while a tap calls /// [onTap]; with no [onTap] it only passes pointers through. class ChatPeekGesture extends StatelessWidget { const ChatPeekGesture({super.key, required this.onTap, required this.child}); final VoidCallback? onTap; final Widget child; @override Widget build(BuildContext context) => RawGestureDetector( behavior: HitTestBehavior.opaque, gestures: onTap == null ? const {} : { ChatPeekTapRecognizer: GestureRecognizerFactoryWithHandlers( () => ChatPeekTapRecognizer(debugOwner: this), (recognizer) => recognizer.onTap = onTap, ), }, child: child, ); }