From 4f77b7eb94c223d87ac498ecbe8479e7a5370466 Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Fri, 31 Aug 2018 17:35:55 -0700 Subject: [PATCH 1/2] Add a gestureRecognizers parameter to GoogleMap. These gesture recognizers participates in gesture arenas on behalf of the map. This allows fine grain control over which gestures are dispatched to the map. --- .../google_maps_flutter/example/lib/main.dart | 2 + .../example/lib/scrolling_map.dart | 126 ++++++++++++++++++ .../lib/google_maps_flutter.dart | 1 + .../lib/src/google_map.dart | 25 +++- 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 packages/google_maps_flutter/example/lib/scrolling_map.dart diff --git a/packages/google_maps_flutter/example/lib/main.dart b/packages/google_maps_flutter/example/lib/main.dart index eab390c1bacf..b77407344e15 100644 --- a/packages/google_maps_flutter/example/lib/main.dart +++ b/packages/google_maps_flutter/example/lib/main.dart @@ -8,12 +8,14 @@ import 'map_ui.dart'; import 'move_camera.dart'; import 'page.dart'; import 'place_marker.dart'; +import 'scrolling_map.dart'; final List _allPages = [ MapUiPage(), AnimateCameraPage(), MoveCameraPage(), PlaceMarkerPage(), + ScrollingMapPage(), ]; class MapsDemo extends StatelessWidget { diff --git a/packages/google_maps_flutter/example/lib/scrolling_map.dart b/packages/google_maps_flutter/example/lib/scrolling_map.dart new file mode 100644 index 000000000000..d0f63671e95e --- /dev/null +++ b/packages/google_maps_flutter/example/lib/scrolling_map.dart @@ -0,0 +1,126 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:google_maps_flutter/google_maps_flutter.dart'; + +import 'page.dart'; + +class ScrollingMapPage extends Page { + ScrollingMapPage() : super(const Icon(Icons.list), 'Scrolling map'); + + @override + Widget build(BuildContext context) { + return const ScrollingMapBody(); + } +} + +class ScrollingMapBody extends StatelessWidget { + const ScrollingMapBody(); + + final LatLng center = const LatLng(32.080664, 34.9563837); + + @override + Widget build(BuildContext context) { + return ListView( + children: [ + new Card( + child: new Padding( + padding: const EdgeInsets.symmetric(vertical: 30.0), + child: new Column( + children: [ + const Padding( + padding: EdgeInsets.only(bottom: 12.0), + child: Text('This map consumes all touch events.'), + ), + Center( + child: SizedBox( + width: 300.0, + height: 300.0, + child: GoogleMap( + onMapCreated: onMapCreated, + options: new GoogleMapOptions( + cameraPosition: CameraPosition( + target: center, + zoom: 11.0, + ), + ), + gestureRecognizers: [ + new EagerGestureRecognizer() + ], + ), + ), + ), + ], + ), + ), + ), + new Card( + child: new Padding( + padding: const EdgeInsets.symmetric(vertical: 30.0), + child: new Column( + children: [ + const Text('This map doesn\'t consume the vertical drags.'), + const Padding( + padding: EdgeInsets.only(bottom: 12.0), + child: + Text('It still gets other gestures (e.g scale or tap).'), + ), + Center( + child: SizedBox( + width: 300.0, + height: 300.0, + child: GoogleMap( + onMapCreated: onMapCreated, + options: new GoogleMapOptions( + cameraPosition: CameraPosition( + target: center, + zoom: 11.0, + ), + ), + gestureRecognizers: [ + ScaleGestureRecognizer() + ], + ), + ), + ), + ], + ), + ), + ), + ], + ); + } + + void onMapCreated(GoogleMapController controller) { + controller.addMarker(MarkerOptions( + position: LatLng( + center.latitude, + center.longitude, + ), + infoWindowText: const InfoWindowText('An interesting location', '*'), + )); + } +} + +class EagerGestureRecognizer extends OneSequenceGestureRecognizer { + @override + void addPointer(PointerDownEvent event) { + startTrackingPointer(event.pointer); + } + + @override + String get debugDescription => 'eager'; + + @override + void didStopTrackingLastPointer(int pointer) {} + + @override + void handleEvent(PointerEvent event) { + resolve(GestureDisposition.accepted); + stopTrackingPointer(event.pointer); + } +} diff --git a/packages/google_maps_flutter/lib/google_maps_flutter.dart b/packages/google_maps_flutter/lib/google_maps_flutter.dart index 781541705f7b..085f0ac9041e 100644 --- a/packages/google_maps_flutter/lib/google_maps_flutter.dart +++ b/packages/google_maps_flutter/lib/google_maps_flutter.dart @@ -8,6 +8,7 @@ import 'dart:async'; import 'dart:ui'; import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; diff --git a/packages/google_maps_flutter/lib/src/google_map.dart b/packages/google_maps_flutter/lib/src/google_map.dart index 22d0bdcf1806..be75bbb99b7d 100644 --- a/packages/google_maps_flutter/lib/src/google_map.dart +++ b/packages/google_maps_flutter/lib/src/google_map.dart @@ -1,14 +1,34 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + part of google_maps_flutter; typedef void MapCreatedCallback(GoogleMapController controller); class GoogleMap extends StatefulWidget { - GoogleMap({@required this.onMapCreated, GoogleMapOptions options}) - : this.options = GoogleMapOptions.defaultOptions.copyWith(options); + GoogleMap({ + @required this.onMapCreated, + GoogleMapOptions options, + this.gestureRecognizers = const [], + }) : assert(gestureRecognizers != null), + this.options = GoogleMapOptions.defaultOptions.copyWith(options); final MapCreatedCallback onMapCreated; + final GoogleMapOptions options; + /// Which gestures should be consumed by the map. + /// + /// It is possible for other gesture recognizers to be competing with the map on pointer + /// events, e.g if the map is inside a [ListView] the [ListView] will want to handle + /// vertical drags. The map will claim gestures that are recognized by any of the + /// recognizers on this list. + /// + /// When this list is empty, the map will only handle pointer events for gestures that + /// were not claimed by any other gesture recognizer. + final List gestureRecognizers; + @override State createState() => new _GoogleMapState(); } @@ -20,6 +40,7 @@ class _GoogleMapState extends State { return AndroidView( viewType: 'plugins.flutter.io/google_maps', onPlatformViewCreated: onPlatformViewCreated, + gestureRecognizers: widget.gestureRecognizers, creationParams: widget.options._toJson(), creationParamsCodec: const StandardMessageCodec(), ); From 8eb702278215bad267c29f92db60cd47daae8053 Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Thu, 6 Sep 2018 11:26:37 -0700 Subject: [PATCH 2/2] Use the framework's EagerGestureDetector --- .../example/lib/scrolling_map.dart | 21 +------------------ 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/packages/google_maps_flutter/example/lib/scrolling_map.dart b/packages/google_maps_flutter/example/lib/scrolling_map.dart index d0f63671e95e..c94a25472ac6 100644 --- a/packages/google_maps_flutter/example/lib/scrolling_map.dart +++ b/packages/google_maps_flutter/example/lib/scrolling_map.dart @@ -10,7 +10,7 @@ import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'page.dart'; class ScrollingMapPage extends Page { - ScrollingMapPage() : super(const Icon(Icons.list), 'Scrolling map'); + ScrollingMapPage() : super(const Icon(Icons.map), 'Scrolling map'); @override Widget build(BuildContext context) { @@ -105,22 +105,3 @@ class ScrollingMapBody extends StatelessWidget { )); } } - -class EagerGestureRecognizer extends OneSequenceGestureRecognizer { - @override - void addPointer(PointerDownEvent event) { - startTrackingPointer(event.pointer); - } - - @override - String get debugDescription => 'eager'; - - @override - void didStopTrackingLastPointer(int pointer) {} - - @override - void handleEvent(PointerEvent event) { - resolve(GestureDisposition.accepted); - stopTrackingPointer(event.pointer); - } -}