From 1f25acf7fcc18934d5db060840688b0ae8d502f1 Mon Sep 17 00:00:00 2001 From: James McIntosh Date: Wed, 20 Oct 2021 13:47:22 +1300 Subject: [PATCH] [google_maps_flutter] Clean Java test, consolidate Marker example. (#4400) * Do not mock internal class * Merge "Marker" examples into a single page. --- .../googlemaps/MarkersControllerTest.java | 10 +- .../example/lib/drag_marker.dart | 156 ----------- .../google_maps_flutter/example/lib/main.dart | 2 - .../example/lib/place_marker.dart | 246 +++++++++--------- 4 files changed, 131 insertions(+), 283 deletions(-) delete mode 100644 packages/google_maps_flutter/google_maps_flutter/example/lib/drag_marker.dart diff --git a/packages/google_maps_flutter/google_maps_flutter/android/src/test/java/io/flutter/plugins/googlemaps/MarkersControllerTest.java b/packages/google_maps_flutter/google_maps_flutter/android/src/test/java/io/flutter/plugins/googlemaps/MarkersControllerTest.java index afe91d77e2be..3ca78e7674d7 100644 --- a/packages/google_maps_flutter/google_maps_flutter/android/src/test/java/io/flutter/plugins/googlemaps/MarkersControllerTest.java +++ b/packages/google_maps_flutter/google_maps_flutter/android/src/test/java/io/flutter/plugins/googlemaps/MarkersControllerTest.java @@ -9,7 +9,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; -import com.google.android.gms.internal.maps.zzt; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; @@ -35,8 +34,7 @@ public void controller_OnMarkerDragStart() { final GoogleMap googleMap = mock(GoogleMap.class); controller.setGoogleMap(googleMap); - final zzt z = mock(zzt.class); - final Marker marker = new Marker(z); + final Marker marker = mock(Marker.class); final String googleMarkerId = "abc123"; @@ -69,8 +67,7 @@ public void controller_OnMarkerDragEnd() { final GoogleMap googleMap = mock(GoogleMap.class); controller.setGoogleMap(googleMap); - final zzt z = mock(zzt.class); - final Marker marker = new Marker(z); + final Marker marker = mock(Marker.class); final String googleMarkerId = "abc123"; @@ -103,8 +100,7 @@ public void controller_OnMarkerDrag() { final GoogleMap googleMap = mock(GoogleMap.class); controller.setGoogleMap(googleMap); - final zzt z = mock(zzt.class); - final Marker marker = new Marker(z); + final Marker marker = mock(Marker.class); final String googleMarkerId = "abc123"; diff --git a/packages/google_maps_flutter/google_maps_flutter/example/lib/drag_marker.dart b/packages/google_maps_flutter/google_maps_flutter/example/lib/drag_marker.dart deleted file mode 100644 index 2c7929115247..000000000000 --- a/packages/google_maps_flutter/google_maps_flutter/example/lib/drag_marker.dart +++ /dev/null @@ -1,156 +0,0 @@ -// 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. - -// ignore_for_file: public_member_api_docs - -import 'dart:math'; - -import 'package:flutter/material.dart'; -import 'package:google_maps_flutter/google_maps_flutter.dart'; - -import 'page.dart'; - -class DragMarkerPage extends GoogleMapExampleAppPage { - DragMarkerPage() : super(const Icon(Icons.drag_handle), 'Drag marker'); - - @override - Widget build(BuildContext context) { - return const DragMarkerBody(); - } -} - -class DragMarkerBody extends StatefulWidget { - const DragMarkerBody(); - - @override - State createState() => DragMarkerBodyState(); -} - -typedef MarkerUpdateAction = Marker Function(Marker marker); - -class DragMarkerBodyState extends State { - DragMarkerBodyState(); - static const LatLng center = LatLng(-33.86711, 151.1947171); - - GoogleMapController? controller; - Map markers = {}; - MarkerId? selectedMarker; - int _markerIdCounter = 1; - LatLng? markerPosition; - - void _onMapCreated(GoogleMapController controller) { - this.controller = controller; - } - - void _onMarkerTapped(MarkerId markerId) { - final Marker? tappedMarker = markers[markerId]; - if (tappedMarker != null) { - setState(() { - if (markers.containsKey(selectedMarker)) { - final Marker resetOld = markers[selectedMarker]! - .copyWith(iconParam: BitmapDescriptor.defaultMarker); - markers[selectedMarker!] = resetOld; - } - selectedMarker = markerId; - final Marker newMarker = tappedMarker.copyWith( - iconParam: BitmapDescriptor.defaultMarkerWithHue( - BitmapDescriptor.hueGreen, - ), - ); - markers[markerId] = newMarker; - }); - } - } - - void _onMarkerDrag(MarkerId markerId, LatLng newPosition) async { - setState(() { - this.markerPosition = newPosition; - }); - } - - void _add() { - final int markerCount = markers.length; - - if (markerCount == 12) { - return; - } - - final String markerIdVal = 'marker_id_$_markerIdCounter'; - _markerIdCounter++; - final MarkerId markerId = MarkerId(markerIdVal); - - final Marker marker = Marker( - draggable: true, - markerId: markerId, - position: LatLng( - center.latitude + sin(_markerIdCounter * pi / 6.0) / 20.0, - center.longitude + cos(_markerIdCounter * pi / 6.0) / 20.0, - ), - infoWindow: InfoWindow(title: markerIdVal, snippet: '*'), - onTap: () => _onMarkerTapped(markerId), - onDrag: (LatLng position) => _onMarkerDrag(markerId, position), - ); - - setState(() { - markers[markerId] = marker; - }); - } - - void _remove() { - setState(() { - if (markers.containsKey(selectedMarker)) { - markers.remove(selectedMarker); - } - }); - } - - @override - Widget build(BuildContext context) { - return Column( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - Expanded( - child: Center( - child: GoogleMap( - onMapCreated: _onMapCreated, - initialCameraPosition: const CameraPosition( - target: center, - zoom: 11.0, - ), - markers: markers.values.toSet(), - ), - ), - ), - Container( - height: 30, - padding: EdgeInsets.only(left: 12, right: 12), - child: Row( - mainAxisSize: MainAxisSize.max, - children: [ - markerPosition == null - ? Container() - : Expanded(child: Text("lat: ${markerPosition!.latitude}")), - markerPosition == null - ? Container() - : Expanded(child: Text("lng: ${markerPosition!.longitude}")), - ], - ), - ), - Row( - children: [ - TextButton( - child: const Text('add'), - onPressed: _add, - ), - TextButton( - child: const Text('remove'), - onPressed: _remove, - ), - ], - ), - ], - ); - } -} diff --git a/packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart b/packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart index 16f242c9e0ce..15b14db0357a 100644 --- a/packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart +++ b/packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart @@ -5,7 +5,6 @@ // ignore_for_file: public_member_api_docs import 'package:flutter/material.dart'; -import 'package:google_maps_flutter_example/drag_marker.dart'; import 'package:google_maps_flutter_example/lite_mode.dart'; import 'animate_camera.dart'; import 'map_click.dart'; @@ -35,7 +34,6 @@ final List _allPages = [ PlacePolylinePage(), PlacePolygonPage(), PlaceCirclePage(), - DragMarkerPage(), PaddingPage(), SnapshotPage(), LiteModePage(), diff --git a/packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart b/packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart index 53f553eb67f8..4b9496f69a63 100644 --- a/packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart +++ b/packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart @@ -40,6 +40,7 @@ class PlaceMarkerBodyState extends State { Map markers = {}; MarkerId? selectedMarker; int _markerIdCounter = 1; + LatLng? markerPosition; void _onMapCreated(GoogleMapController controller) { this.controller = controller; @@ -67,13 +68,24 @@ class PlaceMarkerBodyState extends State { ), ); markers[markerId] = newMarker; + + markerPosition = null; }); } } + void _onMarkerDrag(MarkerId markerId, LatLng newPosition) async { + setState(() { + this.markerPosition = newPosition; + }); + } + void _onMarkerDragEnd(MarkerId markerId, LatLng newPosition) async { final Marker? tappedMarker = markers[markerId]; if (tappedMarker != null) { + setState(() { + this.markerPosition = null; + }); await showDialog( context: context, builder: (BuildContext context) { @@ -115,12 +127,9 @@ class PlaceMarkerBodyState extends State { center.longitude + cos(_markerIdCounter * pi / 6.0) / 20.0, ), infoWindow: InfoWindow(title: markerIdVal, snippet: '*'), - onTap: () { - _onMarkerTapped(markerId); - }, - onDragEnd: (LatLng position) { - _onMarkerDragEnd(markerId, position); - }, + onTap: () => _onMarkerTapped(markerId), + onDragEnd: (LatLng position) => _onMarkerDragEnd(markerId, position), + onDrag: (LatLng position) => _onMarkerDrag(markerId, position), ); setState(() { @@ -280,14 +289,12 @@ class PlaceMarkerBodyState extends State { @override Widget build(BuildContext context) { final MarkerId? selectedId = selectedMarker; - return Column( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - Center( - child: SizedBox( - width: 300.0, - height: 200.0, + return Stack(children: [ + Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Expanded( child: GoogleMap( onMapCreated: _onMapCreated, initialCameraPosition: const CameraPosition( @@ -297,111 +304,114 @@ class PlaceMarkerBodyState extends State { markers: Set.of(markers.values), ), ), - ), - Expanded( - child: SingleChildScrollView( - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - Row( - children: [ - Column( - children: [ - TextButton( - child: const Text('add'), - onPressed: _add, - ), - TextButton( - child: const Text('remove'), - onPressed: selectedId == null - ? null - : () => _remove(selectedId), - ), - TextButton( - child: const Text('change info'), - onPressed: selectedId == null - ? null - : () => _changeInfo(selectedId), - ), - TextButton( - child: const Text('change info anchor'), - onPressed: selectedId == null - ? null - : () => _changeInfoAnchor(selectedId), - ), - ], - ), - Column( - children: [ - TextButton( - child: const Text('change alpha'), - onPressed: selectedId == null - ? null - : () => _changeAlpha(selectedId), - ), - TextButton( - child: const Text('change anchor'), - onPressed: selectedId == null - ? null - : () => _changeAnchor(selectedId), - ), - TextButton( - child: const Text('toggle draggable'), - onPressed: selectedId == null - ? null - : () => _toggleDraggable(selectedId), - ), - TextButton( - child: const Text('toggle flat'), - onPressed: selectedId == null - ? null - : () => _toggleFlat(selectedId), - ), - TextButton( - child: const Text('change position'), - onPressed: selectedId == null - ? null - : () => _changePosition(selectedId), - ), - TextButton( - child: const Text('change rotation'), - onPressed: selectedId == null - ? null - : () => _changeRotation(selectedId), - ), - TextButton( - child: const Text('toggle visible'), - onPressed: selectedId == null - ? null - : () => _toggleVisible(selectedId), - ), - TextButton( - child: const Text('change zIndex'), - onPressed: selectedId == null - ? null - : () => _changeZIndex(selectedId), - ), - TextButton( - child: const Text('set marker icon'), - onPressed: selectedId == null - ? null - : () { - _getAssetIcon(context).then( - (BitmapDescriptor icon) { - _setMarkerIcon(selectedId, icon); - }, - ); - }, - ), - ], - ), - ], - ) - ], - ), + Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + TextButton( + child: const Text('Add'), + onPressed: _add, + ), + TextButton( + child: const Text('Remove'), + onPressed: + selectedId == null ? null : () => _remove(selectedId), + ), + ], + ), + Wrap( + alignment: WrapAlignment.spaceEvenly, + children: [ + TextButton( + child: const Text('change info'), + onPressed: + selectedId == null ? null : () => _changeInfo(selectedId), + ), + TextButton( + child: const Text('change info anchor'), + onPressed: selectedId == null + ? null + : () => _changeInfoAnchor(selectedId), + ), + TextButton( + child: const Text('change alpha'), + onPressed: + selectedId == null ? null : () => _changeAlpha(selectedId), + ), + TextButton( + child: const Text('change anchor'), + onPressed: + selectedId == null ? null : () => _changeAnchor(selectedId), + ), + TextButton( + child: const Text('toggle draggable'), + onPressed: selectedId == null + ? null + : () => _toggleDraggable(selectedId), + ), + TextButton( + child: const Text('toggle flat'), + onPressed: + selectedId == null ? null : () => _toggleFlat(selectedId), + ), + TextButton( + child: const Text('change position'), + onPressed: selectedId == null + ? null + : () => _changePosition(selectedId), + ), + TextButton( + child: const Text('change rotation'), + onPressed: selectedId == null + ? null + : () => _changeRotation(selectedId), + ), + TextButton( + child: const Text('toggle visible'), + onPressed: selectedId == null + ? null + : () => _toggleVisible(selectedId), + ), + TextButton( + child: const Text('change zIndex'), + onPressed: + selectedId == null ? null : () => _changeZIndex(selectedId), + ), + TextButton( + child: const Text('set marker icon'), + onPressed: selectedId == null + ? null + : () { + _getAssetIcon(context).then( + (BitmapDescriptor icon) { + _setMarkerIcon(selectedId, icon); + }, + ); + }, + ), + ], + ), + ], + ), + Visibility( + visible: markerPosition != null, + child: Container( + color: Colors.white70, + height: 30, + padding: EdgeInsets.only(left: 12, right: 12), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceAround, + mainAxisSize: MainAxisSize.max, + children: [ + markerPosition == null + ? Container() + : Expanded(child: Text("lat: ${markerPosition!.latitude}")), + markerPosition == null + ? Container() + : Expanded(child: Text("lng: ${markerPosition!.longitude}")), + ], ), ), - ], - ); + ), + ]); } }