Skip to content

Commit

Permalink
[google_maps_flutter] Switch to using new structured options interfac…
Browse files Browse the repository at this point in the history
…e methods (flutter#5825)
  • Loading branch information
stuartmorgan authored and yutaaraki-toydium committed Aug 12, 2022
1 parent 7622b79 commit 56b5ff8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 2.1.8

* Switches to new platform interface versions of `buildView` and
`updateOptions`.
* Ignores unnecessary import warnings in preparation for [upcoming Flutter changes](https://github.com/flutter/flutter/pull/104231).

## 2.1.7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ class GoogleMapController {
/// platform side.
///
/// The returned [Future] completes after listeners have been notified.
Future<void> _updateMapOptions(Map<String, dynamic> optionsUpdate) {
assert(optionsUpdate != null);
Future<void> _updateMapConfiguration(MapConfiguration update) {
return GoogleMapsFlutterPlatform.instance
.updateMapOptions(optionsUpdate, mapId: mapId);
.updateMapConfiguration(update, mapId: mapId);
}

/// Updates marker configuration.
Expand Down
156 changes: 44 additions & 112 deletions packages/google_maps_flutter/google_maps_flutter/lib/src/google_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,30 +294,34 @@ class _GoogleMapState extends State<GoogleMap> {
Map<PolygonId, Polygon> _polygons = <PolygonId, Polygon>{};
Map<PolylineId, Polyline> _polylines = <PolylineId, Polyline>{};
Map<CircleId, Circle> _circles = <CircleId, Circle>{};
late _GoogleMapOptions _googleMapOptions;
late MapConfiguration _mapConfiguration;

@override
Widget build(BuildContext context) {
return GoogleMapsFlutterPlatform.instance.buildViewWithTextDirection(
return GoogleMapsFlutterPlatform.instance.buildViewWithConfiguration(
_mapId,
onPlatformViewCreated,
textDirection: widget.layoutDirection ??
Directionality.maybeOf(context) ??
TextDirection.ltr,
initialCameraPosition: widget.initialCameraPosition,
markers: widget.markers,
polygons: widget.polygons,
polylines: widget.polylines,
circles: widget.circles,
gestureRecognizers: widget.gestureRecognizers,
mapOptions: _googleMapOptions.toMap(),
widgetConfiguration: MapWidgetConfiguration(
textDirection: widget.layoutDirection ??
Directionality.maybeOf(context) ??
TextDirection.ltr,
initialCameraPosition: widget.initialCameraPosition,
gestureRecognizers: widget.gestureRecognizers,
),
mapObjects: MapObjects(
markers: widget.markers,
polygons: widget.polygons,
polylines: widget.polylines,
circles: widget.circles,
),
mapConfiguration: _mapConfiguration,
);
}

@override
void initState() {
super.initState();
_googleMapOptions = _GoogleMapOptions.fromWidget(widget);
_mapConfiguration = _configurationFromMapWidget(widget);
_markers = keyByMarkerId(widget.markers);
_polygons = keyByPolygonId(widget.polygons);
_polylines = keyByPolylineId(widget.polylines);
Expand Down Expand Up @@ -347,16 +351,15 @@ class _GoogleMapState extends State<GoogleMap> {
}

Future<void> _updateOptions() async {
final _GoogleMapOptions newOptions = _GoogleMapOptions.fromWidget(widget);
final Map<String, dynamic> updates =
_googleMapOptions.updatesMap(newOptions);
final MapConfiguration newConfig = _configurationFromMapWidget(widget);
final MapConfiguration updates = newConfig.diffFrom(_mapConfiguration);
if (updates.isEmpty) {
return;
}
final GoogleMapController controller = await _controller.future;
// ignore: unawaited_futures
controller._updateMapOptions(updates);
_googleMapOptions = newOptions;
controller._updateMapConfiguration(updates);
_mapConfiguration = newConfig;
}

Future<void> _updateMarkers() async {
Expand Down Expand Up @@ -524,98 +527,27 @@ class _GoogleMapState extends State<GoogleMap> {
}
}

/// Configuration options for the GoogleMaps user interface.
class _GoogleMapOptions {
_GoogleMapOptions.fromWidget(GoogleMap map)
: compassEnabled = map.compassEnabled,
mapToolbarEnabled = map.mapToolbarEnabled,
cameraTargetBounds = map.cameraTargetBounds,
mapType = map.mapType,
minMaxZoomPreference = map.minMaxZoomPreference,
rotateGesturesEnabled = map.rotateGesturesEnabled,
scrollGesturesEnabled = map.scrollGesturesEnabled,
tiltGesturesEnabled = map.tiltGesturesEnabled,
trackCameraPosition = map.onCameraMove != null,
zoomControlsEnabled = map.zoomControlsEnabled,
zoomGesturesEnabled = map.zoomGesturesEnabled,
liteModeEnabled = map.liteModeEnabled,
myLocationEnabled = map.myLocationEnabled,
myLocationButtonEnabled = map.myLocationButtonEnabled,
padding = map.padding,
indoorViewEnabled = map.indoorViewEnabled,
trafficEnabled = map.trafficEnabled,
buildingsEnabled = map.buildingsEnabled,
assert(!map.liteModeEnabled || Platform.isAndroid);

final bool compassEnabled;

final bool mapToolbarEnabled;

final CameraTargetBounds cameraTargetBounds;

final MapType mapType;

final MinMaxZoomPreference minMaxZoomPreference;

final bool rotateGesturesEnabled;

final bool scrollGesturesEnabled;

final bool tiltGesturesEnabled;

final bool trackCameraPosition;

final bool zoomControlsEnabled;

final bool zoomGesturesEnabled;

final bool liteModeEnabled;

final bool myLocationEnabled;

final bool myLocationButtonEnabled;

final EdgeInsets padding;

final bool indoorViewEnabled;

final bool trafficEnabled;

final bool buildingsEnabled;

Map<String, dynamic> toMap() {
return <String, dynamic>{
'compassEnabled': compassEnabled,
'mapToolbarEnabled': mapToolbarEnabled,
'cameraTargetBounds': cameraTargetBounds.toJson(),
'mapType': mapType.index,
'minMaxZoomPreference': minMaxZoomPreference.toJson(),
'rotateGesturesEnabled': rotateGesturesEnabled,
'scrollGesturesEnabled': scrollGesturesEnabled,
'tiltGesturesEnabled': tiltGesturesEnabled,
'zoomControlsEnabled': zoomControlsEnabled,
'zoomGesturesEnabled': zoomGesturesEnabled,
'liteModeEnabled': liteModeEnabled,
'trackCameraPosition': trackCameraPosition,
'myLocationEnabled': myLocationEnabled,
'myLocationButtonEnabled': myLocationButtonEnabled,
'padding': <double>[
padding.top,
padding.left,
padding.bottom,
padding.right,
],
'indoorEnabled': indoorViewEnabled,
'trafficEnabled': trafficEnabled,
'buildingsEnabled': buildingsEnabled,
};
}

Map<String, dynamic> updatesMap(_GoogleMapOptions newOptions) {
final Map<String, dynamic> prevOptionsMap = toMap();

return newOptions.toMap()
..removeWhere(
(String key, dynamic value) => prevOptionsMap[key] == value);
}
/// Builds a [MapConfiguration] from the given [map].
MapConfiguration _configurationFromMapWidget(GoogleMap map) {
assert(!map.liteModeEnabled || Platform.isAndroid);
return MapConfiguration(
compassEnabled: map.compassEnabled,
mapToolbarEnabled: map.mapToolbarEnabled,
cameraTargetBounds: map.cameraTargetBounds,
mapType: map.mapType,
minMaxZoomPreference: map.minMaxZoomPreference,
rotateGesturesEnabled: map.rotateGesturesEnabled,
scrollGesturesEnabled: map.scrollGesturesEnabled,
tiltGesturesEnabled: map.tiltGesturesEnabled,
trackCameraPosition: map.onCameraMove != null,
zoomControlsEnabled: map.zoomControlsEnabled,
zoomGesturesEnabled: map.zoomGesturesEnabled,
liteModeEnabled: map.liteModeEnabled,
myLocationEnabled: map.myLocationEnabled,
myLocationButtonEnabled: map.myLocationButtonEnabled,
padding: map.padding,
indoorViewEnabled: map.indoorViewEnabled,
trafficEnabled: map.trafficEnabled,
buildingsEnabled: map.buildingsEnabled,
);
}
4 changes: 2 additions & 2 deletions packages/google_maps_flutter/google_maps_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_maps_flutter
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
repository: https://github.com/flutter/plugins/tree/main/packages/google_maps_flutter/google_maps_flutter
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
version: 2.1.7
version: 2.1.8

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -21,7 +21,7 @@ dependencies:
flutter:
sdk: flutter
flutter_plugin_android_lifecycle: ^2.0.1
google_maps_flutter_platform_interface: ^2.1.2
google_maps_flutter_platform_interface: ^2.2.0

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import 'dart:async';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -90,8 +88,8 @@ class TestGoogleMapsFlutterPlatform extends GoogleMapsFlutterPlatform {
Future<void> init(int mapId) async {}

@override
Future<void> updateMapOptions(
Map<String, dynamic> optionsUpdate, {
Future<void> updateMapConfiguration(
MapConfiguration update, {
required int mapId,
}) async {}

Expand Down Expand Up @@ -278,18 +276,12 @@ class TestGoogleMapsFlutterPlatform extends GoogleMapsFlutterPlatform {
}

@override
Widget buildView(
Widget buildViewWithConfiguration(
int creationId,
PlatformViewCreatedCallback onPlatformViewCreated, {
required CameraPosition initialCameraPosition,
Set<Marker> markers = const <Marker>{},
Set<Polygon> polygons = const <Polygon>{},
Set<Polyline> polylines = const <Polyline>{},
Set<Circle> circles = const <Circle>{},
Set<TileOverlay> tileOverlays = const <TileOverlay>{},
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers =
const <Factory<OneSequenceGestureRecognizer>>{},
Map<String, dynamic> mapOptions = const <String, dynamic>{},
required MapWidgetConfiguration widgetConfiguration,
MapObjects mapObjects = const MapObjects(),
MapConfiguration mapConfiguration = const MapConfiguration(),
}) {
onPlatformViewCreated(0);
createdIds.add(creationId);
Expand Down

0 comments on commit 56b5ff8

Please sign in to comment.