Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit b8d4cce

Browse files
committed
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.
1 parent 15ef622 commit b8d4cce

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

packages/google_maps_flutter/example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import 'map_ui.dart';
88
import 'move_camera.dart';
99
import 'page.dart';
1010
import 'place_marker.dart';
11+
import 'scrolling_map.dart';
1112

1213
final List<Page> _allPages = <Page>[
1314
MapUiPage(),
1415
AnimateCameraPage(),
1516
MoveCameraPage(),
1617
PlaceMarkerPage(),
18+
ScrollingMapPage(),
1719
];
1820

1921
class MapsDemo extends StatelessWidget {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/gestures.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter/rendering.dart';
8+
import 'package:google_maps_flutter/google_maps_flutter.dart';
9+
10+
import 'page.dart';
11+
12+
class ScrollingMapPage extends Page {
13+
ScrollingMapPage() : super(const Icon(Icons.list), 'Scrolling map');
14+
15+
@override
16+
Widget build(BuildContext context) {
17+
return const ScrollingMapBody();
18+
}
19+
}
20+
21+
class ScrollingMapBody extends StatelessWidget {
22+
const ScrollingMapBody();
23+
24+
final LatLng center = const LatLng(32.080664, 34.9563837);
25+
26+
@override
27+
Widget build(BuildContext context) {
28+
return ListView(
29+
children: <Widget>[
30+
new Card(
31+
child: new Padding(
32+
padding: const EdgeInsets.symmetric(vertical: 30.0),
33+
child: new Column(
34+
children: <Widget>[
35+
const Padding(
36+
padding: EdgeInsets.only(bottom: 12.0),
37+
child: Text('This map consumes all touch events.'),
38+
),
39+
Center(
40+
child: SizedBox(
41+
width: 300.0,
42+
height: 300.0,
43+
child: GoogleMap(
44+
onMapCreated: onMapCreated,
45+
options: new GoogleMapOptions(
46+
cameraPosition: CameraPosition(
47+
target: center,
48+
zoom: 11.0,
49+
),
50+
),
51+
gestureRecognizers: <OneSequenceGestureRecognizer>[
52+
new EagerGestureRecognizer()
53+
],
54+
),
55+
),
56+
),
57+
],
58+
),
59+
),
60+
),
61+
new Card(
62+
child: new Padding(
63+
padding: const EdgeInsets.symmetric(vertical: 30.0),
64+
child: new Column(
65+
children: <Widget>[
66+
const Text('This map doesn\'t consume the vertical drags.'),
67+
const Padding(
68+
padding: EdgeInsets.only(bottom: 12.0),
69+
child: Text('It still gets other gestures (e.g scale or tap).'),
70+
),
71+
Center(
72+
child: SizedBox(
73+
width: 300.0,
74+
height: 300.0,
75+
child: GoogleMap(
76+
onMapCreated: onMapCreated,
77+
options: new GoogleMapOptions(
78+
cameraPosition: CameraPosition(
79+
target: center,
80+
zoom: 11.0,
81+
),
82+
),
83+
gestureRecognizers: <OneSequenceGestureRecognizer>[
84+
ScaleGestureRecognizer()
85+
],
86+
),
87+
),
88+
),
89+
],
90+
),
91+
),
92+
),
93+
],
94+
);
95+
}
96+
97+
void onMapCreated(GoogleMapController controller) {
98+
controller.addMarker(MarkerOptions(
99+
position: LatLng(
100+
center.latitude,
101+
center.longitude,
102+
),
103+
infoWindowText: const InfoWindowText('An interesting location', '*'),
104+
));
105+
}
106+
}
107+
108+
class EagerGestureRecognizer extends OneSequenceGestureRecognizer {
109+
@override
110+
void addPointer(PointerDownEvent event) {
111+
startTrackingPointer(event.pointer);
112+
}
113+
114+
@override
115+
String get debugDescription => 'eager';
116+
117+
@override
118+
void didStopTrackingLastPointer(int pointer) {}
119+
120+
@override
121+
void handleEvent(PointerEvent event) {
122+
resolve(GestureDisposition.accepted);
123+
stopTrackingPointer(event.pointer);
124+
}
125+
}

packages/google_maps_flutter/lib/google_maps_flutter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:async';
88
import 'dart:ui';
99

1010
import 'package:flutter/foundation.dart';
11+
import 'package:flutter/gestures.dart';
1112
import 'package:flutter/material.dart';
1213
import 'package:flutter/services.dart';
1314

packages/google_maps_flutter/lib/src/google_map.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
15
part of google_maps_flutter;
26

37
typedef void MapCreatedCallback(GoogleMapController controller);
48

59
class GoogleMap extends StatefulWidget {
6-
GoogleMap({@required this.onMapCreated, GoogleMapOptions options})
7-
: this.options = GoogleMapOptions.defaultOptions.copyWith(options);
10+
GoogleMap({
11+
@required this.onMapCreated,
12+
GoogleMapOptions options,
13+
this.gestureRecognizers = const <OneSequenceGestureRecognizer>[],
14+
}) : assert(gestureRecognizers != null),
15+
this.options = GoogleMapOptions.defaultOptions.copyWith(options);
816

917
final MapCreatedCallback onMapCreated;
18+
1019
final GoogleMapOptions options;
1120

21+
/// Which gestures should be consumed by the map.
22+
///
23+
/// It is possible for other gesture recognizers to be competing with the map on pointer
24+
/// events, e.g if the map is inside a [ListView] the [ListView] will want to handle
25+
/// vertical drags. The map will claim gestures that are recognized by any of the
26+
/// recognizers on this list.
27+
///
28+
/// When this list is empty, the map will only handle pointer events for gestures that
29+
/// were not claimed by any other gesture recognizer.
30+
final List<OneSequenceGestureRecognizer> gestureRecognizers;
31+
1232
@override
1333
State createState() => new _GoogleMapState();
1434
}
@@ -20,6 +40,7 @@ class _GoogleMapState extends State<GoogleMap> {
2040
return AndroidView(
2141
viewType: 'plugins.flutter.io/google_maps',
2242
onPlatformViewCreated: onPlatformViewCreated,
43+
gestureRecognizers: widget.gestureRecognizers,
2344
creationParams: widget.options._toJson(),
2445
creationParamsCodec: const StandardMessageCodec(),
2546
);

0 commit comments

Comments
 (0)