diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md index 016ec9e968e3..c1a7c95abfdb 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md @@ -1,5 +1,10 @@ -## NEXT +## 2.2.0 +* Adds new versions of `buildView` and `updateOptions` that take a new option + class instead of a dictionary, to remove the cross-package dependency on + magic string keys. +* Adopts several parameter objects in the new `buildView` variant to + future-proof it against future changes. * Ignores unnecessary import warnings in preparation for [upcoming Flutter changes](https://github.com/flutter/flutter/pull/104231). ## 2.1.7 diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/method_channel/method_channel_google_maps_flutter.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/method_channel/method_channel_google_maps_flutter.dart index 60df6f52499f..a34ee48ac79a 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/method_channel/method_channel_google_maps_flutter.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/method_channel/method_channel_google_maps_flutter.dart @@ -16,6 +16,7 @@ import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platf import 'package:stream_transform/stream_transform.dart'; import '../types/tile_overlay_updates.dart'; +import '../types/utils/map_configuration_serialization.dart'; /// Error thrown when an unknown map ID is provided to a method channel API. class UnknownMapIDError extends Error { @@ -484,28 +485,22 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform { /// Defaults to false. bool useAndroidViewSurface = false; - @override - Widget buildViewWithTextDirection( + Widget _buildView( int creationId, PlatformViewCreatedCallback onPlatformViewCreated, { - required CameraPosition initialCameraPosition, - required TextDirection textDirection, - Set markers = const {}, - Set polygons = const {}, - Set polylines = const {}, - Set circles = const {}, - Set tileOverlays = const {}, - Set>? gestureRecognizers, + required MapWidgetConfiguration widgetConfiguration, + MapObjects mapObjects = const MapObjects(), Map mapOptions = const {}, }) { final Map creationParams = { - 'initialCameraPosition': initialCameraPosition.toMap(), + 'initialCameraPosition': + widgetConfiguration.initialCameraPosition.toMap(), 'options': mapOptions, - 'markersToAdd': serializeMarkerSet(markers), - 'polygonsToAdd': serializePolygonSet(polygons), - 'polylinesToAdd': serializePolylineSet(polylines), - 'circlesToAdd': serializeCircleSet(circles), - 'tileOverlaysToAdd': serializeTileOverlaySet(tileOverlays), + 'markersToAdd': serializeMarkerSet(mapObjects.markers), + 'polygonsToAdd': serializePolygonSet(mapObjects.polygons), + 'polylinesToAdd': serializePolylineSet(mapObjects.polylines), + 'circlesToAdd': serializeCircleSet(mapObjects.circles), + 'tileOverlaysToAdd': serializeTileOverlaySet(mapObjects.tileOverlays), }; if (defaultTargetPlatform == TargetPlatform.android) { @@ -518,8 +513,7 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform { ) { return AndroidViewSurface( controller: controller as AndroidViewController, - gestureRecognizers: gestureRecognizers ?? - const >{}, + gestureRecognizers: widgetConfiguration.gestureRecognizers, hitTestBehavior: PlatformViewHitTestBehavior.opaque, ); }, @@ -528,7 +522,7 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform { PlatformViewsService.initSurfaceAndroidView( id: params.id, viewType: 'plugins.flutter.io/google_maps', - layoutDirection: textDirection, + layoutDirection: widgetConfiguration.textDirection, creationParams: creationParams, creationParamsCodec: const StandardMessageCodec(), onFocus: () => params.onFocusChanged(true), @@ -548,7 +542,7 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform { return AndroidView( viewType: 'plugins.flutter.io/google_maps', onPlatformViewCreated: onPlatformViewCreated, - gestureRecognizers: gestureRecognizers, + gestureRecognizers: widgetConfiguration.gestureRecognizers, creationParams: creationParams, creationParamsCodec: const StandardMessageCodec(), ); @@ -557,7 +551,7 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform { return UiKitView( viewType: 'plugins.flutter.io/google_maps', onPlatformViewCreated: onPlatformViewCreated, - gestureRecognizers: gestureRecognizers, + gestureRecognizers: widgetConfiguration.gestureRecognizers, creationParams: creationParams, creationParamsCodec: const StandardMessageCodec(), ); @@ -567,6 +561,53 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform { '$defaultTargetPlatform is not yet supported by the maps plugin'); } + @override + Widget buildViewWithConfiguration( + int creationId, + PlatformViewCreatedCallback onPlatformViewCreated, { + required MapWidgetConfiguration widgetConfiguration, + MapConfiguration mapConfiguration = const MapConfiguration(), + MapObjects mapObjects = const MapObjects(), + }) { + return _buildView( + creationId, + onPlatformViewCreated, + widgetConfiguration: widgetConfiguration, + mapObjects: mapObjects, + mapOptions: jsonForMapConfiguration(mapConfiguration), + ); + } + + @override + Widget buildViewWithTextDirection( + int creationId, + PlatformViewCreatedCallback onPlatformViewCreated, { + required CameraPosition initialCameraPosition, + required TextDirection textDirection, + Set markers = const {}, + Set polygons = const {}, + Set polylines = const {}, + Set circles = const {}, + Set tileOverlays = const {}, + Set>? gestureRecognizers, + Map mapOptions = const {}, + }) { + return _buildView( + creationId, + onPlatformViewCreated, + widgetConfiguration: MapWidgetConfiguration( + initialCameraPosition: initialCameraPosition, + textDirection: textDirection), + mapObjects: MapObjects( + markers: markers, + polygons: polygons, + polylines: polylines, + circles: circles, + tileOverlays: tileOverlays), + mapOptions: mapOptions, + ); + } + @override Widget buildView( int creationId, diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/platform_interface/google_maps_flutter_platform.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/platform_interface/google_maps_flutter_platform.dart index 8c36ebe4f6ef..b6b95018d0c4 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/platform_interface/google_maps_flutter_platform.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/platform_interface/google_maps_flutter_platform.dart @@ -13,6 +13,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; +import 'package:google_maps_flutter_platform_interface/src/types/utils/map_configuration_serialization.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; /// The interface that platform-specific implementations of `google_maps_flutter` must extend. @@ -50,7 +51,8 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface { throw UnimplementedError('init() has not been implemented.'); } - /// Updates configuration options of the map user interface. + /// Updates configuration options of the map user interface - deprecated, use + /// updateMapConfiguration instead. /// /// Change listeners are notified once the update has been made on the /// platform side. @@ -63,6 +65,20 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface { throw UnimplementedError('updateMapOptions() has not been implemented.'); } + /// Updates configuration options of the map user interface. + /// + /// Change listeners are notified once the update has been made on the + /// platform side. + /// + /// The returned [Future] completes after listeners have been notified. + Future updateMapConfiguration( + MapConfiguration configuration, { + required int mapId, + }) { + return updateMapOptions(jsonForMapConfiguration(configuration), + mapId: mapId); + } + /// Updates marker configuration. /// /// Change listeners are notified once the update has been made on the @@ -348,7 +364,8 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface { throw UnimplementedError('dispose() has not been implemented.'); } - /// Returns a widget displaying the map view. + /// Returns a widget displaying the map view - deprecated, use + /// [buildViewWithConfiguration] instead. Widget buildView( int creationId, PlatformViewCreatedCallback onPlatformViewCreated, { @@ -367,7 +384,8 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface { throw UnimplementedError('buildView() has not been implemented.'); } - /// Returns a widget displaying the map view. + /// Returns a widget displaying the map view - deprecated, use + /// [buildViewWithConfiguration] instead. /// /// This method is similar to [buildView], but contains a parameter for /// platforms that require a text direction. @@ -381,12 +399,12 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface { PlatformViewCreatedCallback onPlatformViewCreated, { required CameraPosition initialCameraPosition, required TextDirection textDirection, + Set>? gestureRecognizers, Set markers = const {}, Set polygons = const {}, Set polylines = const {}, Set circles = const {}, Set tileOverlays = const {}, - Set>? gestureRecognizers, Map mapOptions = const {}, }) { return buildView( @@ -402,4 +420,27 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface { mapOptions: mapOptions, ); } + + /// Returns a widget displaying the map view. + Widget buildViewWithConfiguration( + int creationId, + PlatformViewCreatedCallback onPlatformViewCreated, { + required MapWidgetConfiguration widgetConfiguration, + MapConfiguration mapConfiguration = const MapConfiguration(), + MapObjects mapObjects = const MapObjects(), + }) { + return buildViewWithTextDirection( + creationId, + onPlatformViewCreated, + initialCameraPosition: widgetConfiguration.initialCameraPosition, + textDirection: widgetConfiguration.textDirection, + markers: mapObjects.markers, + polygons: mapObjects.polygons, + polylines: mapObjects.polylines, + circles: mapObjects.circles, + tileOverlays: mapObjects.tileOverlays, + gestureRecognizers: widgetConfiguration.gestureRecognizers, + mapOptions: jsonForMapConfiguration(mapConfiguration), + ); + } } diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart index c43baf42db45..0ccc3e624abe 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart @@ -18,10 +18,8 @@ class BitmapDescriptor { const BitmapDescriptor._(this._json); /// The inverse of .toJson. - // This is needed in Web to re-hydrate BitmapDescriptors that have been - // transformed to JSON for transport. - // TODO(stuartmorgan): Clean this up. See - // https://github.com/flutter/flutter/issues/70330 + // TODO(stuartmorgan): Remove this in the next breaking change. + @Deprecated('No longer supported') BitmapDescriptor.fromJson(Object json) : _json = json { assert(_json is List); final List jsonList = json as List; diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_configuration.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_configuration.dart new file mode 100644 index 000000000000..4b43caffe5b6 --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_configuration.dart @@ -0,0 +1,248 @@ +// 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. + +import 'package:flutter/widgets.dart'; + +import 'ui.dart'; + +/// Configuration options for the GoogleMaps user interface. +@immutable +class MapConfiguration { + /// Creates a new configuration instance with the given options. + /// + /// Any options that aren't passed will be null, which allows this to serve + /// as either a full configuration selection, or an update to an existing + /// configuration where only non-null values are updated. + const MapConfiguration({ + this.compassEnabled, + this.mapToolbarEnabled, + this.cameraTargetBounds, + this.mapType, + this.minMaxZoomPreference, + this.rotateGesturesEnabled, + this.scrollGesturesEnabled, + this.tiltGesturesEnabled, + this.trackCameraPosition, + this.zoomControlsEnabled, + this.zoomGesturesEnabled, + this.liteModeEnabled, + this.myLocationEnabled, + this.myLocationButtonEnabled, + this.padding, + this.indoorViewEnabled, + this.trafficEnabled, + this.buildingsEnabled, + }); + + /// True if the compass UI should be shown. + final bool? compassEnabled; + + /// True if the map toolbar should be shown. + final bool? mapToolbarEnabled; + + /// The bounds to display. + final CameraTargetBounds? cameraTargetBounds; + + /// The type of the map. + final MapType? mapType; + + /// The prefered zoom range. + final MinMaxZoomPreference? minMaxZoomPreference; + + /// True if rotate gestures should be enabled. + final bool? rotateGesturesEnabled; + + /// True if scroll gestures should be enabled. + final bool? scrollGesturesEnabled; + + /// True if tilt gestures should be enabled. + final bool? tiltGesturesEnabled; + + /// True if camera position changes should trigger notifications. + final bool? trackCameraPosition; + + /// True if zoom controls should be displayed. + final bool? zoomControlsEnabled; + + /// True if zoom gestures should be enabled. + final bool? zoomGesturesEnabled; + + /// True if the map should use Lite Mode, showing a limited-interactivity + /// bitmap, on supported platforms. + final bool? liteModeEnabled; + + /// True if the current location should be tracked and displayed. + final bool? myLocationEnabled; + + /// True if the control to jump to the current location should be displayed. + final bool? myLocationButtonEnabled; + + /// The padding for the map display. + final EdgeInsets? padding; + + /// True if indoor map views should be enabled. + final bool? indoorViewEnabled; + + /// True if the traffic overlay should be enabled. + final bool? trafficEnabled; + + /// True if 3D building display should be enabled. + final bool? buildingsEnabled; + + /// Returns a new options object containing only the values of this instance + /// that are different from [other]. + MapConfiguration diffFrom(MapConfiguration other) { + return MapConfiguration( + compassEnabled: + compassEnabled != other.compassEnabled ? compassEnabled : null, + mapToolbarEnabled: mapToolbarEnabled != other.mapToolbarEnabled + ? mapToolbarEnabled + : null, + cameraTargetBounds: cameraTargetBounds != other.cameraTargetBounds + ? cameraTargetBounds + : null, + mapType: mapType != other.mapType ? mapType : null, + minMaxZoomPreference: minMaxZoomPreference != other.minMaxZoomPreference + ? minMaxZoomPreference + : null, + rotateGesturesEnabled: + rotateGesturesEnabled != other.rotateGesturesEnabled + ? rotateGesturesEnabled + : null, + scrollGesturesEnabled: + scrollGesturesEnabled != other.scrollGesturesEnabled + ? scrollGesturesEnabled + : null, + tiltGesturesEnabled: tiltGesturesEnabled != other.tiltGesturesEnabled + ? tiltGesturesEnabled + : null, + trackCameraPosition: trackCameraPosition != other.trackCameraPosition + ? trackCameraPosition + : null, + zoomControlsEnabled: zoomControlsEnabled != other.zoomControlsEnabled + ? zoomControlsEnabled + : null, + zoomGesturesEnabled: zoomGesturesEnabled != other.zoomGesturesEnabled + ? zoomGesturesEnabled + : null, + liteModeEnabled: + liteModeEnabled != other.liteModeEnabled ? liteModeEnabled : null, + myLocationEnabled: myLocationEnabled != other.myLocationEnabled + ? myLocationEnabled + : null, + myLocationButtonEnabled: + myLocationButtonEnabled != other.myLocationButtonEnabled + ? myLocationButtonEnabled + : null, + padding: padding != other.padding ? padding : null, + indoorViewEnabled: indoorViewEnabled != other.indoorViewEnabled + ? indoorViewEnabled + : null, + trafficEnabled: + trafficEnabled != other.trafficEnabled ? trafficEnabled : null, + buildingsEnabled: + buildingsEnabled != other.buildingsEnabled ? buildingsEnabled : null, + ); + } + + /// Returns a copy of this instance with any non-null settings form [diff] + /// replacing the previous values. + MapConfiguration applyDiff(MapConfiguration diff) { + return MapConfiguration( + compassEnabled: diff.compassEnabled ?? compassEnabled, + mapToolbarEnabled: diff.mapToolbarEnabled ?? mapToolbarEnabled, + cameraTargetBounds: diff.cameraTargetBounds ?? cameraTargetBounds, + mapType: diff.mapType ?? mapType, + minMaxZoomPreference: diff.minMaxZoomPreference ?? minMaxZoomPreference, + rotateGesturesEnabled: + diff.rotateGesturesEnabled ?? rotateGesturesEnabled, + scrollGesturesEnabled: + diff.scrollGesturesEnabled ?? scrollGesturesEnabled, + tiltGesturesEnabled: diff.tiltGesturesEnabled ?? tiltGesturesEnabled, + trackCameraPosition: diff.trackCameraPosition ?? trackCameraPosition, + zoomControlsEnabled: diff.zoomControlsEnabled ?? zoomControlsEnabled, + zoomGesturesEnabled: diff.zoomGesturesEnabled ?? zoomGesturesEnabled, + liteModeEnabled: diff.liteModeEnabled ?? liteModeEnabled, + myLocationEnabled: diff.myLocationEnabled ?? myLocationEnabled, + myLocationButtonEnabled: + diff.myLocationButtonEnabled ?? myLocationButtonEnabled, + padding: diff.padding ?? padding, + indoorViewEnabled: diff.indoorViewEnabled ?? indoorViewEnabled, + trafficEnabled: diff.trafficEnabled ?? trafficEnabled, + buildingsEnabled: diff.buildingsEnabled ?? buildingsEnabled, + ); + } + + /// True if no options are set. + bool get isEmpty => + compassEnabled == null && + mapToolbarEnabled == null && + cameraTargetBounds == null && + mapType == null && + minMaxZoomPreference == null && + rotateGesturesEnabled == null && + scrollGesturesEnabled == null && + tiltGesturesEnabled == null && + trackCameraPosition == null && + zoomControlsEnabled == null && + zoomGesturesEnabled == null && + liteModeEnabled == null && + myLocationEnabled == null && + myLocationButtonEnabled == null && + padding == null && + indoorViewEnabled == null && + trafficEnabled == null && + buildingsEnabled == null; + + @override + bool operator ==(Object other) { + if (identical(this, other)) { + return true; + } + if (other.runtimeType != runtimeType) { + return false; + } + return other is MapConfiguration && + compassEnabled == other.compassEnabled && + mapToolbarEnabled == other.mapToolbarEnabled && + cameraTargetBounds == other.cameraTargetBounds && + mapType == other.mapType && + minMaxZoomPreference == other.minMaxZoomPreference && + rotateGesturesEnabled == other.rotateGesturesEnabled && + scrollGesturesEnabled == other.scrollGesturesEnabled && + tiltGesturesEnabled == other.tiltGesturesEnabled && + trackCameraPosition == other.trackCameraPosition && + zoomControlsEnabled == other.zoomControlsEnabled && + zoomGesturesEnabled == other.zoomGesturesEnabled && + liteModeEnabled == other.liteModeEnabled && + myLocationEnabled == other.myLocationEnabled && + myLocationButtonEnabled == other.myLocationButtonEnabled && + padding == other.padding && + indoorViewEnabled == other.indoorViewEnabled && + trafficEnabled == other.trafficEnabled && + buildingsEnabled == other.buildingsEnabled; + } + + @override + int get hashCode => Object.hash( + compassEnabled, + mapToolbarEnabled, + cameraTargetBounds, + mapType, + minMaxZoomPreference, + rotateGesturesEnabled, + scrollGesturesEnabled, + tiltGesturesEnabled, + trackCameraPosition, + zoomControlsEnabled, + zoomGesturesEnabled, + liteModeEnabled, + myLocationEnabled, + myLocationButtonEnabled, + padding, + indoorViewEnabled, + trafficEnabled, + buildingsEnabled, + ); +} diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_objects.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_objects.dart new file mode 100644 index 000000000000..56f80e8312dd --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_objects.dart @@ -0,0 +1,31 @@ +// 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 'package:flutter/foundation.dart'; + +import 'types.dart'; + +/// A container object for all the types of maps objects. +/// +/// This is intended for use as a parameter in platform interface methods, to +/// allow adding new object types to existing methods. +@immutable +class MapObjects { + /// Creates a new set of map objects with all the given object types. + const MapObjects({ + this.markers = const {}, + this.polygons = const {}, + this.polylines = const {}, + this.circles = const {}, + this.tileOverlays = const {}, + }); + + final Set markers; + final Set polygons; + final Set polylines; + final Set circles; + final Set tileOverlays; +} diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_widget_configuration.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_widget_configuration.dart new file mode 100644 index 000000000000..029af9901661 --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_widget_configuration.dart @@ -0,0 +1,32 @@ +// 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. + +import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; + +import 'types.dart'; + +/// A container object for configuration options when building a widget. +/// +/// This is intended for use as a parameter in platform interface methods, to +/// allow adding new configuration options to existing methods. +@immutable +class MapWidgetConfiguration { + /// Creates a new configuration with all the given settings. + const MapWidgetConfiguration({ + required this.initialCameraPosition, + required this.textDirection, + this.gestureRecognizers = const >{}, + }); + + /// The initial camera position to display. + final CameraPosition initialCameraPosition; + + /// The text direction for the widget. + final TextDirection textDirection; + + /// Gesture recognizers to add to the widget. + final Set> gestureRecognizers; +} diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/types.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/types.dart index 1e1bef8ee6c0..0beb7d747ec8 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/types.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/types.dart @@ -11,6 +11,9 @@ export 'circle.dart'; export 'circle_updates.dart'; export 'joint_type.dart'; export 'location.dart'; +export 'map_configuration.dart'; +export 'map_objects.dart'; +export 'map_widget_configuration.dart'; export 'maps_object.dart'; export 'maps_object_updates.dart'; export 'marker.dart'; @@ -25,7 +28,7 @@ export 'tile.dart'; export 'tile_overlay.dart'; export 'tile_provider.dart'; export 'ui.dart'; -// Export the utils, they're used by the Widget +// Export the utils used by the Widget export 'utils/circle.dart'; export 'utils/marker.dart'; export 'utils/polygon.dart'; diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/utils/map_configuration_serialization.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/utils/map_configuration_serialization.dart new file mode 100644 index 000000000000..01f4fa054570 --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/utils/map_configuration_serialization.dart @@ -0,0 +1,62 @@ +// 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. + +import 'package:flutter/material.dart'; + +import '../map_configuration.dart'; + +/// Returns a JSON representation of [config]. +/// +/// This is intended for two purposes: +/// - Conversion of [MapConfiguration] to the map options dictionary used by +/// legacy platform interface methods. +/// - Conversion of [MapConfiguration] to the default method channel +/// implementation's representation. +/// +/// Both of these are parts of the public interface, so any change to the +/// representation other than adding a new field requires a breaking change to +/// the package. +Map jsonForMapConfiguration(MapConfiguration config) { + final EdgeInsets? padding = config.padding; + return { + if (config.compassEnabled != null) 'compassEnabled': config.compassEnabled!, + if (config.mapToolbarEnabled != null) + 'mapToolbarEnabled': config.mapToolbarEnabled!, + if (config.cameraTargetBounds != null) + 'cameraTargetBounds': config.cameraTargetBounds!.toJson(), + if (config.mapType != null) 'mapType': config.mapType!.index, + if (config.minMaxZoomPreference != null) + 'minMaxZoomPreference': config.minMaxZoomPreference!.toJson(), + if (config.rotateGesturesEnabled != null) + 'rotateGesturesEnabled': config.rotateGesturesEnabled!, + if (config.scrollGesturesEnabled != null) + 'scrollGesturesEnabled': config.scrollGesturesEnabled!, + if (config.tiltGesturesEnabled != null) + 'tiltGesturesEnabled': config.tiltGesturesEnabled!, + if (config.zoomControlsEnabled != null) + 'zoomControlsEnabled': config.zoomControlsEnabled!, + if (config.zoomGesturesEnabled != null) + 'zoomGesturesEnabled': config.zoomGesturesEnabled!, + if (config.liteModeEnabled != null) + 'liteModeEnabled': config.liteModeEnabled!, + if (config.trackCameraPosition != null) + 'trackCameraPosition': config.trackCameraPosition!, + if (config.myLocationEnabled != null) + 'myLocationEnabled': config.myLocationEnabled!, + if (config.myLocationButtonEnabled != null) + 'myLocationButtonEnabled': config.myLocationButtonEnabled!, + if (padding != null) + 'padding': [ + padding.top, + padding.left, + padding.bottom, + padding.right, + ], + if (config.indoorViewEnabled != null) + 'indoorEnabled': config.indoorViewEnabled!, + if (config.trafficEnabled != null) 'trafficEnabled': config.trafficEnabled!, + if (config.buildingsEnabled != null) + 'buildingsEnabled': config.buildingsEnabled!, + }; +} diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml index 759daf2bb1cb..2b01e6244210 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/google_maps_fl issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22 # NOTE: We strongly prefer non-breaking changes, even at the expense of a # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes -version: 2.1.7 +version: 2.2.0 environment: sdk: '>=2.12.0 <3.0.0' diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/platform_interface/google_maps_flutter_platform_test.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/platform_interface/google_maps_flutter_platform_test.dart index 0899bb6a8fb2..d185aabe1a5c 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/platform_interface/google_maps_flutter_platform_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/platform_interface/google_maps_flutter_platform_test.dart @@ -57,6 +57,25 @@ void main() { ); }, ); + + test( + 'default implementation of `buildViewWithConfiguration` delegates to `buildViewWithTextDirection`', + () { + final GoogleMapsFlutterPlatform platform = + BuildViewGoogleMapsFlutterPlatform(); + expect( + platform.buildViewWithConfiguration( + 0, + (_) {}, + widgetConfiguration: const MapWidgetConfiguration( + initialCameraPosition: CameraPosition(target: LatLng(0.0, 0.0)), + textDirection: TextDirection.ltr, + ), + ), + isA(), + ); + }, + ); }); } diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/map_configuration_test.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/map_configuration_test.dart new file mode 100644 index 000000000000..edd1fd091073 --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/map_configuration_test.dart @@ -0,0 +1,412 @@ +// 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. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; + +void main() { + group('diffs', () { + // A options instance with every field set, to test diffs against. + final MapConfiguration diffBase = MapConfiguration( + compassEnabled: false, + mapToolbarEnabled: false, + cameraTargetBounds: CameraTargetBounds(LatLngBounds( + northeast: const LatLng(30, 20), southwest: const LatLng(10, 40))), + mapType: MapType.normal, + minMaxZoomPreference: const MinMaxZoomPreference(1.0, 10.0), + rotateGesturesEnabled: false, + scrollGesturesEnabled: false, + tiltGesturesEnabled: false, + trackCameraPosition: false, + zoomControlsEnabled: false, + zoomGesturesEnabled: false, + liteModeEnabled: false, + myLocationEnabled: false, + myLocationButtonEnabled: false, + padding: const EdgeInsets.all(5.0), + indoorViewEnabled: false, + trafficEnabled: false, + buildingsEnabled: false, + ); + + test('only include changed fields', () async { + const MapConfiguration nullOptions = MapConfiguration(); + + // Everything should be null since nothing changed. + expect(diffBase.diffFrom(diffBase), nullOptions); + }); + + test('only apply non-null fields', () async { + const MapConfiguration smallDiff = MapConfiguration(compassEnabled: true); + + final MapConfiguration updated = diffBase.applyDiff(smallDiff); + + // The diff should be updated. + expect(updated.compassEnabled, true); + // Spot check that other fields weren't stomped. + expect(updated.mapToolbarEnabled, isNot(null)); + expect(updated.cameraTargetBounds, isNot(null)); + expect(updated.mapType, isNot(null)); + expect(updated.zoomControlsEnabled, isNot(null)); + expect(updated.liteModeEnabled, isNot(null)); + expect(updated.padding, isNot(null)); + expect(updated.trafficEnabled, isNot(null)); + }); + + test('handle compassEnabled', () async { + const MapConfiguration diff = MapConfiguration(compassEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.compassEnabled, true); + }); + + test('handle mapToolbarEnabled', () async { + const MapConfiguration diff = MapConfiguration(mapToolbarEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.mapToolbarEnabled, true); + }); + + test('handle cameraTargetBounds', () async { + final CameraTargetBounds newBounds = CameraTargetBounds(LatLngBounds( + northeast: const LatLng(55, 15), southwest: const LatLng(5, 15))); + final MapConfiguration diff = + MapConfiguration(cameraTargetBounds: newBounds); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.cameraTargetBounds, newBounds); + }); + + test('handle mapType', () async { + const MapConfiguration diff = + MapConfiguration(mapType: MapType.satellite); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.mapType, MapType.satellite); + }); + + test('handle minMaxZoomPreference', () async { + const MinMaxZoomPreference newZoomPref = MinMaxZoomPreference(3.3, 4.5); + const MapConfiguration diff = + MapConfiguration(minMaxZoomPreference: newZoomPref); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.minMaxZoomPreference, newZoomPref); + }); + + test('handle rotateGesturesEnabled', () async { + const MapConfiguration diff = + MapConfiguration(rotateGesturesEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.rotateGesturesEnabled, true); + }); + + test('handle scrollGesturesEnabled', () async { + const MapConfiguration diff = + MapConfiguration(scrollGesturesEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.scrollGesturesEnabled, true); + }); + + test('handle tiltGesturesEnabled', () async { + const MapConfiguration diff = MapConfiguration(tiltGesturesEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.tiltGesturesEnabled, true); + }); + + test('handle trackCameraPosition', () async { + const MapConfiguration diff = MapConfiguration(trackCameraPosition: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.trackCameraPosition, true); + }); + + test('handle zoomControlsEnabled', () async { + const MapConfiguration diff = MapConfiguration(zoomControlsEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.zoomControlsEnabled, true); + }); + + test('handle zoomGesturesEnabled', () async { + const MapConfiguration diff = MapConfiguration(zoomGesturesEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.zoomGesturesEnabled, true); + }); + + test('handle liteModeEnabled', () async { + const MapConfiguration diff = MapConfiguration(liteModeEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.liteModeEnabled, true); + }); + + test('handle myLocationEnabled', () async { + const MapConfiguration diff = MapConfiguration(myLocationEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.myLocationEnabled, true); + }); + + test('handle myLocationButtonEnabled', () async { + const MapConfiguration diff = + MapConfiguration(myLocationButtonEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.myLocationButtonEnabled, true); + }); + + test('handle padding', () async { + const EdgeInsets newPadding = + EdgeInsets.symmetric(vertical: 1.0, horizontal: 3.0); + const MapConfiguration diff = MapConfiguration(padding: newPadding); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.padding, newPadding); + }); + + test('handle indoorViewEnabled', () async { + const MapConfiguration diff = MapConfiguration(indoorViewEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.indoorViewEnabled, true); + }); + + test('handle trafficEnabled', () async { + const MapConfiguration diff = MapConfiguration(trafficEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.trafficEnabled, true); + }); + + test('handle buildingsEnabled', () async { + const MapConfiguration diff = MapConfiguration(buildingsEnabled: true); + + const MapConfiguration empty = MapConfiguration(); + final MapConfiguration updated = diffBase.applyDiff(diff); + + // A diff applied to empty options should be the diff itself. + expect(empty.applyDiff(diff), diff); + // A diff applied to non-empty options should update that field. + expect(updated.buildingsEnabled, true); + }); + }); + + group('isEmpty', () { + test('is true for empty', () async { + const MapConfiguration nullOptions = MapConfiguration(); + + expect(nullOptions.isEmpty, true); + }); + + test('is false with compassEnabled', () async { + const MapConfiguration diff = MapConfiguration(compassEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with mapToolbarEnabled', () async { + const MapConfiguration diff = MapConfiguration(mapToolbarEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with cameraTargetBounds', () async { + final CameraTargetBounds newBounds = CameraTargetBounds(LatLngBounds( + northeast: const LatLng(55, 15), southwest: const LatLng(5, 15))); + final MapConfiguration diff = + MapConfiguration(cameraTargetBounds: newBounds); + + expect(diff.isEmpty, false); + }); + + test('is false with mapType', () async { + const MapConfiguration diff = + MapConfiguration(mapType: MapType.satellite); + + expect(diff.isEmpty, false); + }); + + test('is false with minMaxZoomPreference', () async { + const MinMaxZoomPreference newZoomPref = MinMaxZoomPreference(3.3, 4.5); + const MapConfiguration diff = + MapConfiguration(minMaxZoomPreference: newZoomPref); + + expect(diff.isEmpty, false); + }); + + test('is false with rotateGesturesEnabled', () async { + const MapConfiguration diff = + MapConfiguration(rotateGesturesEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with scrollGesturesEnabled', () async { + const MapConfiguration diff = + MapConfiguration(scrollGesturesEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with tiltGesturesEnabled', () async { + const MapConfiguration diff = MapConfiguration(tiltGesturesEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with trackCameraPosition', () async { + const MapConfiguration diff = MapConfiguration(trackCameraPosition: true); + + expect(diff.isEmpty, false); + }); + + test('is false with zoomControlsEnabled', () async { + const MapConfiguration diff = MapConfiguration(zoomControlsEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with zoomGesturesEnabled', () async { + const MapConfiguration diff = MapConfiguration(zoomGesturesEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with liteModeEnabled', () async { + const MapConfiguration diff = MapConfiguration(liteModeEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with myLocationEnabled', () async { + const MapConfiguration diff = MapConfiguration(myLocationEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with myLocationButtonEnabled', () async { + const MapConfiguration diff = + MapConfiguration(myLocationButtonEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with padding', () async { + const EdgeInsets newPadding = + EdgeInsets.symmetric(vertical: 1.0, horizontal: 3.0); + const MapConfiguration diff = MapConfiguration(padding: newPadding); + + expect(diff.isEmpty, false); + }); + + test('is false with indoorViewEnabled', () async { + const MapConfiguration diff = MapConfiguration(indoorViewEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with trafficEnabled', () async { + const MapConfiguration diff = MapConfiguration(trafficEnabled: true); + + expect(diff.isEmpty, false); + }); + + test('is false with buildingsEnabled', () async { + const MapConfiguration diff = MapConfiguration(buildingsEnabled: true); + + expect(diff.isEmpty, false); + }); + }); +} diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/utils/map_configuration_serialization_test.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/utils/map_configuration_serialization_test.dart new file mode 100644 index 000000000000..71a0f8c4b1b1 --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/utils/map_configuration_serialization_test.dart @@ -0,0 +1,75 @@ +// 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. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; +import 'package:google_maps_flutter_platform_interface/src/types/utils/map_configuration_serialization.dart'; + +void main() { + test('empty serialization', () async { + const MapConfiguration config = MapConfiguration(); + + final Map json = jsonForMapConfiguration(config); + + expect(json.isEmpty, true); + }); + + test('complete serialization', () async { + final MapConfiguration config = MapConfiguration( + compassEnabled: false, + mapToolbarEnabled: false, + cameraTargetBounds: CameraTargetBounds(LatLngBounds( + northeast: const LatLng(30, 20), southwest: const LatLng(10, 40))), + mapType: MapType.normal, + minMaxZoomPreference: const MinMaxZoomPreference(1.0, 10.0), + rotateGesturesEnabled: false, + scrollGesturesEnabled: false, + tiltGesturesEnabled: false, + trackCameraPosition: false, + zoomControlsEnabled: false, + zoomGesturesEnabled: false, + liteModeEnabled: false, + myLocationEnabled: false, + myLocationButtonEnabled: false, + padding: const EdgeInsets.all(5.0), + indoorViewEnabled: false, + trafficEnabled: false, + buildingsEnabled: false, + ); + + final Map json = jsonForMapConfiguration(config); + + // This uses literals instead of toJson() for the expectations on + // sub-objects, because if the serialization of any of those objects were + // ever to change MapConfiguration would need to update to serialize those + // objects manually to preserve the format, in order to avoid breaking + // implementations. + expect(json, { + 'compassEnabled': false, + 'mapToolbarEnabled': false, + 'cameraTargetBounds': [ + [ + [10.0, 40.0], + [30.0, 20.0] + ] + ], + 'mapType': 1, + 'minMaxZoomPreference': [1.0, 10.0], + 'rotateGesturesEnabled': false, + 'scrollGesturesEnabled': false, + 'tiltGesturesEnabled': false, + 'zoomControlsEnabled': false, + 'zoomGesturesEnabled': false, + 'liteModeEnabled': false, + 'trackCameraPosition': false, + 'myLocationEnabled': false, + 'myLocationButtonEnabled': false, + 'padding': [5.0, 5.0, 5.0, 5.0], + 'indoorEnabled': false, + 'trafficEnabled': false, + 'buildingsEnabled': false + }); + }); +}