Skip to content

Commit

Permalink
[google_map_flutter] Add style to widget - platform interface (flutte…
Browse files Browse the repository at this point in the history
…r#6197)

Platform interface portion of flutter#6192

Adds `style` to `MapConfiguration` and adds new `getStyleError` method.

Part of Fixes flutter/flutter#66207
  • Loading branch information
stuartmorgan authored Feb 26, 2024
1 parent 25d9c9d commit 353086c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## NEXT
## 2.5.0

* Adds `style` to the `MapConfiguration` to allow setting style as part of
map creation.
* Adds `getStyleError` to the platform interface, to allow asynchronous access
to style errors that occur during initialization.
* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.

## 2.4.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface {
throw UnimplementedError('dispose() has not been implemented.');
}

/// If the last attempt to set the style via [MapConfiguration.style] failed,
/// returns the error information, otherwise returns null.
Future<String?> getStyleError({required int mapId}) async {
return null;
}

/// Returns a widget displaying the map view - deprecated, use
/// [buildViewWithConfiguration] instead.
Widget buildView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MapConfiguration {
this.trafficEnabled,
this.buildingsEnabled,
this.cloudMapId,
this.style,
});

/// This setting controls how the API handles gestures on the map. Web only.
Expand Down Expand Up @@ -113,6 +114,11 @@ class MapConfiguration {
/// for more details.
final String? cloudMapId;

/// Locally configured JSON style.
///
/// To clear a previously set style, set this to an empty string.
final String? style;

/// Returns a new options object containing only the values of this instance
/// that are different from [other].
MapConfiguration diffFrom(MapConfiguration other) {
Expand Down Expand Up @@ -174,6 +180,7 @@ class MapConfiguration {
buildingsEnabled:
buildingsEnabled != other.buildingsEnabled ? buildingsEnabled : null,
cloudMapId: cloudMapId != other.cloudMapId ? cloudMapId : null,
style: style != other.style ? style : null,
);
}

Expand Down Expand Up @@ -206,6 +213,7 @@ class MapConfiguration {
trafficEnabled: diff.trafficEnabled ?? trafficEnabled,
buildingsEnabled: diff.buildingsEnabled ?? buildingsEnabled,
cloudMapId: diff.cloudMapId ?? cloudMapId,
style: diff.style ?? style,
);
}

Expand All @@ -231,7 +239,8 @@ class MapConfiguration {
indoorViewEnabled == null &&
trafficEnabled == null &&
buildingsEnabled == null &&
cloudMapId == null;
cloudMapId == null &&
style == null;

@override
bool operator ==(Object other) {
Expand Down Expand Up @@ -262,7 +271,8 @@ class MapConfiguration {
indoorViewEnabled == other.indoorViewEnabled &&
trafficEnabled == other.trafficEnabled &&
buildingsEnabled == other.buildingsEnabled &&
cloudMapId == other.cloudMapId;
cloudMapId == other.cloudMapId &&
style == other.style;
}

@override
Expand All @@ -288,5 +298,6 @@ class MapConfiguration {
trafficEnabled,
buildingsEnabled,
cloudMapId,
style,
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ Map<String, Object> jsonForMapConfiguration(MapConfiguration config) {
if (config.buildingsEnabled != null)
'buildingsEnabled': config.buildingsEnabled!,
if (config.cloudMapId != null) 'cloudMapId': config.cloudMapId!,
if (config.style != null) 'style': config.style!,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/google_maps_f
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.4.3
version: 2.5.0

environment:
sdk: ^3.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void main() {
);
},
);

test(
'default implementation of `getStyleError` returns null',
() async {
final GoogleMapsFlutterPlatform platform =
BuildViewGoogleMapsFlutterPlatform();
expect(await platform.getStyleError(mapId: 0), null);
},
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void main() {
indoorViewEnabled: false,
trafficEnabled: false,
buildingsEnabled: false,
style: 'diff base style',
);

test('only include changed fields', () async {
Expand Down Expand Up @@ -408,6 +409,23 @@ void main() {
// The hash code should change.
expect(empty.hashCode, isNot(diff.hashCode));
});

test('handle style', () async {
const String aStlye = 'a style';
const MapConfiguration diff = MapConfiguration(style: aStlye);

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);
// The diff from empty options should be the diff itself.
expect(diff.diffFrom(empty), diff);
// A diff applied to non-empty options should update that field.
expect(updated.style, aStlye);
// The hash code should change.
expect(empty.hashCode, isNot(diff.hashCode));
});
});

group('isEmpty', () {
Expand Down Expand Up @@ -541,5 +559,11 @@ void main() {

expect(diff.isEmpty, false);
});

test('is false with style', () async {
const MapConfiguration diff = MapConfiguration(style: 'a style');

expect(diff.isEmpty, false);
});
});
}

0 comments on commit 353086c

Please sign in to comment.