From e68dab42353ba27f24ac478a01bc51f3f2f14727 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 26 Jul 2018 17:28:57 +0300 Subject: [PATCH] Add double-tap to zoom Added the necessary code to support double-tap to zoom. Also added a dispose function. --- flutter_map/lib/src/gestures/gestures.dart | 48 ++++++++++++++++++- .../lib/src/map/flutter_map_state.dart | 1 + 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/flutter_map/lib/src/gestures/gestures.dart b/flutter_map/lib/src/gestures/gestures.dart index e83444308..1179af594 100644 --- a/flutter_map/lib/src/gestures/gestures.dart +++ b/flutter_map/lib/src/gestures/gestures.dart @@ -8,7 +8,7 @@ import 'package:latlong/latlong.dart'; import 'package:flutter_map/flutter_map.dart'; abstract class MapGestureMixin extends State - with SingleTickerProviderStateMixin { + with TickerProviderStateMixin { static const double _kMinFlingVelocity = 800.0; LatLng _mapCenterStart; @@ -19,15 +19,22 @@ abstract class MapGestureMixin extends State Animation _flingAnimation; Offset _animationOffset = Offset.zero; + AnimationController _doubleTapController; + Animation _doubleTapAnimation; + FlutterMap get widget; MapState get mapState; MapState get map => mapState; MapOptions get options; + @override void initState() { super.initState(); _controller = new AnimationController(vsync: this) ..addListener(_handleFlingAnimation); + _doubleTapController = new AnimationController( + vsync: this, duration: Duration(milliseconds: 200)) + ..addListener(_handleDoubleTapZoomAnimation); } void handleScaleStart(ScaleStartDetails details) { @@ -111,6 +118,38 @@ abstract class MapGestureMixin extends State options.onTap(latlng); } + void handleDoubleTap() { + ///Currently zooms in the center of the screen + ///TODO: change the newCenter to be where the user tapped, see https://github.com/flutter/flutter/issues/10048 + + _mapZoomStart = map.zoom; + _mapCenterStart = map.center; + + double dScale = 2.0; + for (var i = 0; i < 2; i++) { + dScale = math.sqrt(dScale); + } + + double newZoom = _mapZoomStart * dScale; + + _doubleTapAnimation = new Tween( + begin: _mapZoomStart, + end: newZoom, + ) + .chain(new CurveTween(curve: Curves.fastOutSlowIn)) + .animate(_doubleTapController); + _doubleTapController + ..value = 0.0 + ..forward(); + } + + void _handleDoubleTapZoomAnimation() { + var newCenter = map.project(_mapCenterStart); + setState(() { + map.move(map.unproject(newCenter), _doubleTapAnimation.value); + }); + } + void _handleFlingAnimation() { setState(() { _animationOffset = _flingAnimation.value; @@ -128,4 +167,11 @@ abstract class MapGestureMixin extends State Offset _pointToOffset(Point point) { return new Offset(point.x.toDouble(), point.y.toDouble()); } + + @override + void dispose() { + _controller.dispose(); + _doubleTapController.dispose(); + super.dispose(); + } } diff --git a/flutter_map/lib/src/map/flutter_map_state.dart b/flutter_map/lib/src/map/flutter_map_state.dart index 6ffc660d2..b00b34c9f 100644 --- a/flutter_map/lib/src/map/flutter_map_state.dart +++ b/flutter_map/lib/src/map/flutter_map_state.dart @@ -30,6 +30,7 @@ class FlutterMapState extends MapGestureMixin { onScaleUpdate: handleScaleUpdate, onScaleEnd: handleScaleEnd, onTapUp: handleTapUp, + onDoubleTap: handleDoubleTap, child: new Container( child: new Stack( children: layerWidgets,