Skip to content

Commit

Permalink
[google_maps_flutter_web] Add Marker drag events (flutter#4385)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMcIntosh authored and amantoux committed Dec 11, 2021
1 parent 2a59923 commit c0eb6fd
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 13 deletions.
@@ -1,3 +1,7 @@
## 0.3.2

Add `onDragStart` and `onDrag` to `Marker`

## 0.3.1

* Fix the `getScreenCoordinate(LatLng)` method. [#80710](https://github.com/flutter/flutter/issues/80710)
Expand Down
@@ -1,8 +1,4 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Mocks generated by Mockito 5.0.15 from annotations
// Mocks generated by Mockito 5.0.16 from annotations
// in google_maps_flutter_web_integration_tests/integration_test/google_maps_controller_test.dart.
// Do not manually edit this file.

Expand All @@ -19,6 +15,7 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types

class _FakeGMap_0 extends _i1.Fake implements _i2.GMap {}

Expand Down
Expand Up @@ -404,6 +404,28 @@ void main() {

await _testStreamFiltering(stream, event);
});
testWidgets('onMarkerDragStart', (WidgetTester tester) async {
final event = MarkerDragStartEvent(
mapId,
LatLng(43.3677, -5.8372),
MarkerId('test-123'),
);

final stream = plugin.onMarkerDragStart(mapId: mapId);

await _testStreamFiltering(stream, event);
});
testWidgets('onMarkerDrag', (WidgetTester tester) async {
final event = MarkerDragEvent(
mapId,
LatLng(43.3677, -5.8372),
MarkerId('test-123'),
);

final stream = plugin.onMarkerDrag(mapId: mapId);

await _testStreamFiltering(stream, event);
});
testWidgets('onMarkerDragEnd', (WidgetTester tester) async {
final event = MarkerDragEndEvent(
mapId,
Expand Down
@@ -1,8 +1,4 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Mocks generated by Mockito 5.0.15 from annotations
// Mocks generated by Mockito 5.0.16 from annotations
// in google_maps_flutter_web_integration_tests/integration_test/google_maps_plugin_test.dart.
// Do not manually edit this file.

Expand All @@ -20,6 +16,7 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types

class _FakeStreamController_0<T> extends _i1.Fake
implements _i2.StreamController<T> {}
Expand Down
Expand Up @@ -27,6 +27,14 @@ void main() {
_methodCalledCompleter.complete(true);
}

void onDragStart(gmaps.LatLng _) {
_methodCalledCompleter.complete(true);
}

void onDrag(gmaps.LatLng _) {
_methodCalledCompleter.complete(true);
}

void onDragEnd(gmaps.LatLng _) {
_methodCalledCompleter.complete(true);
}
Expand All @@ -53,6 +61,26 @@ void main() {
expect(await methodCalled, isTrue);
});

testWidgets('onDragStart gets called', (WidgetTester tester) async {
MarkerController(marker: marker, onDragStart: onDragStart);

// Trigger a drag end event...
gmaps.Event.trigger(marker, 'dragstart',
[gmaps.MapMouseEvent()..latLng = gmaps.LatLng(0, 0)]);

expect(await methodCalled, isTrue);
});

testWidgets('onDrag gets called', (WidgetTester tester) async {
MarkerController(marker: marker, onDrag: onDrag);

// Trigger a drag end event...
gmaps.Event.trigger(
marker, 'drag', [gmaps.MapMouseEvent()..latLng = gmaps.LatLng(0, 0)]);

expect(await methodCalled, isTrue);
});

testWidgets('onDragEnd gets called', (WidgetTester tester) async {
MarkerController(marker: marker, onDragEnd: onDragEnd);

Expand Down
Expand Up @@ -4,7 +4,7 @@ publish_to: none
# Tests require flutter beta or greater to run.
environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=2.1.0"
flutter: ">=2.1.0"

dependencies:
google_maps_flutter_web:
Expand Down
Expand Up @@ -240,6 +240,16 @@ class GoogleMapsPlugin extends GoogleMapsFlutterPlatform {
return _events(mapId).whereType<InfoWindowTapEvent>();
}

@override
Stream<MarkerDragStartEvent> onMarkerDragStart({required int mapId}) {
return _events(mapId).whereType<MarkerDragStartEvent>();
}

@override
Stream<MarkerDragEvent> onMarkerDrag({required int mapId}) {
return _events(mapId).whereType<MarkerDragEvent>();
}

@override
Stream<MarkerDragEndEvent> onMarkerDragEnd({required int mapId}) {
return _events(mapId).whereType<MarkerDragEndEvent>();
Expand Down
Expand Up @@ -19,6 +19,8 @@ class MarkerController {
required gmaps.Marker marker,
gmaps.InfoWindow? infoWindow,
bool consumeTapEvents = false,
LatLngCallback? onDragStart,
LatLngCallback? onDrag,
LatLngCallback? onDragEnd,
ui.VoidCallback? onTap,
}) : _marker = marker,
Expand All @@ -29,6 +31,22 @@ class MarkerController {
onTap.call();
});
}
if (onDragStart != null) {
marker.onDragstart.listen((event) {
if (marker != null) {
marker.position = event.latLng;
}
onDragStart.call(event.latLng ?? _nullGmapsLatLng);
});
}
if (onDrag != null) {
marker.onDrag.listen((event) {
if (marker != null) {
marker.position = event.latLng;
}
onDrag.call(event.latLng ?? _nullGmapsLatLng);
});
}
if (onDragEnd != null) {
marker.onDragend.listen((event) {
if (marker != null) {
Expand Down
Expand Up @@ -62,6 +62,12 @@ class MarkersController extends GeometryController {
this.showMarkerInfoWindow(marker.markerId);
_onMarkerTap(marker.markerId);
},
onDragStart: (gmaps.LatLng latLng) {
_onMarkerDragStart(marker.markerId, latLng);
},
onDrag: (gmaps.LatLng latLng) {
_onMarkerDrag(marker.markerId, latLng);
},
onDragEnd: (gmaps.LatLng latLng) {
_onMarkerDragEnd(marker.markerId, latLng);
},
Expand Down Expand Up @@ -140,6 +146,22 @@ class MarkersController extends GeometryController {
_streamController.add(InfoWindowTapEvent(mapId, markerId));
}

void _onMarkerDragStart(MarkerId markerId, gmaps.LatLng latLng) {
_streamController.add(MarkerDragStartEvent(
mapId,
_gmLatLngToLatLng(latLng),
markerId,
));
}

void _onMarkerDrag(MarkerId markerId, gmaps.LatLng latLng) {
_streamController.add(MarkerDragEvent(
mapId,
_gmLatLngToLatLng(latLng),
markerId,
));
}

void _onMarkerDragEnd(MarkerId markerId, gmaps.LatLng latLng) {
_streamController.add(MarkerDragEndEvent(
mapId,
Expand Down
Expand Up @@ -2,7 +2,7 @@ name: google_maps_flutter_web
description: Web platform implementation of google_maps_flutter
repository: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
version: 0.3.1
version: 0.3.2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand All @@ -21,7 +21,7 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
google_maps_flutter_platform_interface: ^2.0.1
google_maps_flutter_platform_interface: ^2.1.2
google_maps: ^5.2.0
meta: ^1.3.0
sanitize_html: ^2.0.0
Expand Down

0 comments on commit c0eb6fd

Please sign in to comment.