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..c94a25472ac6 --- /dev/null +++ b/packages/google_maps_flutter/example/lib/scrolling_map.dart @@ -0,0 +1,107 @@ +// 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.map), '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', '*'), + )); + } +} 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(), );