From 35f2b1a0e23a7ae5834ff0ec85519a8c8b4daab2 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 13 Dec 2022 10:56:22 +0800 Subject: [PATCH] fix pinch zoom on mobile --- lib/filament_gesture_detector.dart | 104 ++++++++++++++--------------- 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/lib/filament_gesture_detector.dart b/lib/filament_gesture_detector.dart index 1b973176..76d0d957 100644 --- a/lib/filament_gesture_detector.dart +++ b/lib/filament_gesture_detector.dart @@ -83,65 +83,61 @@ class _FilamentGestureDetectorState extends State { Widget build(BuildContext context) { return Stack(children: [ Positioned.fill( - child: Listener( - onPointerSignal: (pointerSignal) async { - if (pointerSignal is PointerScrollEvent) { - _scrollTimer?.cancel(); + // pinch zoom on mobile + // couldn't find any equivalent for pointerCount in Listener so we use two widgets: + // - outer is a GestureDetector only for pinch zoom + // - inner is a Listener for all other gestures + child: GestureDetector( + onScaleStart: (d) async { + if (d.pointerCount == 2) { + await widget.controller.zoomEnd(); await widget.controller.zoomBegin(); - await widget.controller.zoomUpdate( - pointerSignal.scrollDelta.dy > 0 ? 100 : -100); - _scrollTimer = Timer(Duration(milliseconds: 100), () { - widget.controller.zoomEnd(); - _scrollTimer = null; - }); - } else { - print(pointerSignal); } }, - onPointerPanZoomStart: (pzs) { - print(pzs); + onScaleEnd: (d) async { + if (d.pointerCount == 2) { + _lastScale = 0; + await widget.controller.zoomEnd(); + } }, - onPointerDown: (d) async { - await _functionStart(d.localPosition.dx, d.localPosition.dy); + onScaleUpdate: (d) async { + if (d.pointerCount == 2) { + if (_lastScale != 0) { + await widget.controller + .zoomUpdate(100 * (_lastScale - d.scale)); + } + } + _lastScale = d.scale; }, - onPointerMove: (d) async { - await _functionUpdate(d.localPosition.dx, d.localPosition.dy); - }, - onPointerUp: (d) async { - await _functionEnd(); - }, - // on - // onScaleStart: (d) async { - // print("SCALE START"); - // if (d.pointerCount == 2) { - // await widget.controller.zoomEnd(); - // await widget.controller.zoomBegin(); - // } else { - // await _functionStart(d.focalPoint.dx, d.focalPoint.dy); - // } - // }, - // onScaleEnd: (d) async { - // print("SCALE END"); - - // if (d.pointerCount == 2) { - // _lastScale = 0; - // await widget.controller.zoomEnd(); - // } else { - // await _functionEnd(); - // } - // }, - // onScaleUpdate: (d) async { - // if (d.pointerCount == 2) { - // if (_lastScale != 0) { - // await widget.controller - // .zoomUpdate(100 * (_lastScale - d.scale)); - // } - // } else { - // await _functionUpdate(d.focalPoint.dx, d.focalPoint.dy); - // } - // _lastScale = d.scale; - // }, - child: widget.child)), + child: Listener( + onPointerSignal: (pointerSignal) async { + // scroll-wheel zoom on desktop + if (pointerSignal is PointerScrollEvent) { + _scrollTimer?.cancel(); + await widget.controller.zoomBegin(); + await widget.controller.zoomUpdate( + pointerSignal.scrollDelta.dy > 0 ? 100 : -100); + _scrollTimer = Timer(Duration(milliseconds: 100), () { + widget.controller.zoomEnd(); + _scrollTimer = null; + }); + } else { + print(pointerSignal); + } + }, + onPointerPanZoomStart: (pzs) {}, + onPointerDown: (d) async { + await _functionStart( + d.localPosition.dx, d.localPosition.dy); + }, + onPointerMove: (d) async { + await _functionUpdate( + d.localPosition.dx, d.localPosition.dy); + }, + onPointerUp: (d) async { + await _functionEnd(); + }, + child: widget.child))), widget.showControls ? Align( alignment: Alignment.bottomRight,