Skip to content

Commit 353086c

Browse files
[google_map_flutter] Add style to widget - platform interface (#6197)
Platform interface portion of #6192 Adds `style` to `MapConfiguration` and adds new `getStyleError` method. Part of Fixes flutter/flutter#66207
1 parent 25d9c9d commit 353086c

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
## NEXT
1+
## 2.5.0
22

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

59
## 2.4.3

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/platform_interface/google_maps_flutter_platform.dart

+6
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface {
362362
throw UnimplementedError('dispose() has not been implemented.');
363363
}
364364

365+
/// If the last attempt to set the style via [MapConfiguration.style] failed,
366+
/// returns the error information, otherwise returns null.
367+
Future<String?> getStyleError({required int mapId}) async {
368+
return null;
369+
}
370+
365371
/// Returns a widget displaying the map view - deprecated, use
366372
/// [buildViewWithConfiguration] instead.
367373
Widget buildView(

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_configuration.dart

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MapConfiguration {
3636
this.trafficEnabled,
3737
this.buildingsEnabled,
3838
this.cloudMapId,
39+
this.style,
3940
});
4041

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

117+
/// Locally configured JSON style.
118+
///
119+
/// To clear a previously set style, set this to an empty string.
120+
final String? style;
121+
116122
/// Returns a new options object containing only the values of this instance
117123
/// that are different from [other].
118124
MapConfiguration diffFrom(MapConfiguration other) {
@@ -174,6 +180,7 @@ class MapConfiguration {
174180
buildingsEnabled:
175181
buildingsEnabled != other.buildingsEnabled ? buildingsEnabled : null,
176182
cloudMapId: cloudMapId != other.cloudMapId ? cloudMapId : null,
183+
style: style != other.style ? style : null,
177184
);
178185
}
179186

@@ -206,6 +213,7 @@ class MapConfiguration {
206213
trafficEnabled: diff.trafficEnabled ?? trafficEnabled,
207214
buildingsEnabled: diff.buildingsEnabled ?? buildingsEnabled,
208215
cloudMapId: diff.cloudMapId ?? cloudMapId,
216+
style: diff.style ?? style,
209217
);
210218
}
211219

@@ -231,7 +239,8 @@ class MapConfiguration {
231239
indoorViewEnabled == null &&
232240
trafficEnabled == null &&
233241
buildingsEnabled == null &&
234-
cloudMapId == null;
242+
cloudMapId == null &&
243+
style == null;
235244

236245
@override
237246
bool operator ==(Object other) {
@@ -262,7 +271,8 @@ class MapConfiguration {
262271
indoorViewEnabled == other.indoorViewEnabled &&
263272
trafficEnabled == other.trafficEnabled &&
264273
buildingsEnabled == other.buildingsEnabled &&
265-
cloudMapId == other.cloudMapId;
274+
cloudMapId == other.cloudMapId &&
275+
style == other.style;
266276
}
267277

268278
@override
@@ -288,5 +298,6 @@ class MapConfiguration {
288298
trafficEnabled,
289299
buildingsEnabled,
290300
cloudMapId,
301+
style,
291302
]);
292303
}

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/utils/map_configuration_serialization.dart

+1
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ Map<String, Object> jsonForMapConfiguration(MapConfiguration config) {
5959
if (config.buildingsEnabled != null)
6060
'buildingsEnabled': config.buildingsEnabled!,
6161
if (config.cloudMapId != null) 'cloudMapId': config.cloudMapId!,
62+
if (config.style != null) 'style': config.style!,
6263
};
6364
}

packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/google_maps_f
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.4.3
7+
version: 2.5.0
88

99
environment:
1010
sdk: ^3.1.0

packages/google_maps_flutter/google_maps_flutter_platform_interface/test/platform_interface/google_maps_flutter_platform_test.dart

+9
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ void main() {
8383
);
8484
},
8585
);
86+
87+
test(
88+
'default implementation of `getStyleError` returns null',
89+
() async {
90+
final GoogleMapsFlutterPlatform platform =
91+
BuildViewGoogleMapsFlutterPlatform();
92+
expect(await platform.getStyleError(mapId: 0), null);
93+
},
94+
);
8695
});
8796
}
8897

packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/map_configuration_test.dart

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void main() {
3333
indoorViewEnabled: false,
3434
trafficEnabled: false,
3535
buildingsEnabled: false,
36+
style: 'diff base style',
3637
);
3738

3839
test('only include changed fields', () async {
@@ -408,6 +409,23 @@ void main() {
408409
// The hash code should change.
409410
expect(empty.hashCode, isNot(diff.hashCode));
410411
});
412+
413+
test('handle style', () async {
414+
const String aStlye = 'a style';
415+
const MapConfiguration diff = MapConfiguration(style: aStlye);
416+
417+
const MapConfiguration empty = MapConfiguration();
418+
final MapConfiguration updated = diffBase.applyDiff(diff);
419+
420+
// A diff applied to empty options should be the diff itself.
421+
expect(empty.applyDiff(diff), diff);
422+
// The diff from empty options should be the diff itself.
423+
expect(diff.diffFrom(empty), diff);
424+
// A diff applied to non-empty options should update that field.
425+
expect(updated.style, aStlye);
426+
// The hash code should change.
427+
expect(empty.hashCode, isNot(diff.hashCode));
428+
});
411429
});
412430

413431
group('isEmpty', () {
@@ -541,5 +559,11 @@ void main() {
541559

542560
expect(diff.isEmpty, false);
543561
});
562+
563+
test('is false with style', () async {
564+
const MapConfiguration diff = MapConfiguration(style: 'a style');
565+
566+
expect(diff.isEmpty, false);
567+
});
544568
});
545569
}

0 commit comments

Comments
 (0)