Skip to content

[google_map_flutter] Add style to widget - platform interface #6197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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);
});
});
}