Skip to content

Commit 1d83c2a

Browse files
ditmanEgor
authored and
Egor
committed
[google_maps_flutter_web] Fix convert.dart issues (flutter#3093)
* Convert initial Polyline/Polygon points correctly. * Convert Flutter Color to CSS correctly. Fixes flutter/flutter#65152 Fixes flutter/flutter#67032
1 parent 1e49095 commit 1d83c2a

File tree

5 files changed

+114
-15
lines changed

5 files changed

+114
-15
lines changed

packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.0+3
2+
3+
* Fix crash when converting initial polylines and polygons. [Issue](https://github.com/flutter/flutter/issues/65152).
4+
* Correctly convert Colors when rendering polylines, polygons and circles. [Issue](https://github.com/flutter/flutter/issues/67032).
5+
16
## 0.1.0+2
27

38
* Fix crash when converting Markers with icon explicitly set to null. [Issue](https://github.com/flutter/flutter/issues/64938).

packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ final _nullLatLngBounds = LatLngBounds(
1313
// Defaults taken from the Google Maps Platform SDK documentation.
1414
final _defaultStrokeColor = Colors.black.value;
1515
final _defaultFillColor = Colors.transparent.value;
16+
final _defaultCssColor = '#000000';
17+
final _defaultCssOpacity = 0.0;
1618

1719
// Indices in the plugin side don't match with the ones
1820
// in the gmaps lib. This translates from plugin -> gmaps.
@@ -24,6 +26,22 @@ final _mapTypeToMapTypeId = {
2426
4: gmaps.MapTypeId.HYBRID,
2527
};
2628

29+
// Converts a [Color] into a valid CSS value #RRGGBB.
30+
String _getCssColor(Color color) {
31+
if (color == null) {
32+
return _defaultCssColor;
33+
}
34+
return '#' + color.value.toRadixString(16).padLeft(8, '0').substring(2);
35+
}
36+
37+
// Extracts the opacity from a [Color].
38+
double _getCssOpacity(Color color) {
39+
if (color == null) {
40+
return _defaultCssOpacity;
41+
}
42+
return color.opacity;
43+
}
44+
2745
// Converts options from the plugin into gmaps.MapOptions that can be used by the JS SDK.
2846
// The following options are not handled here, for various reasons:
2947
// The following are not available in web, because the map doesn't rotate there:
@@ -319,7 +337,7 @@ Set<Polyline> _rawOptionsToInitialPolylines(Map<String, dynamic> rawOptions) {
319337
zIndex: rawPolyline['zIndex'],
320338
width: rawPolyline['width'],
321339
points: rawPolyline['points']
322-
?.map((rawPoint) => LatLng.fromJson(rawPoint))
340+
?.map<LatLng>((rawPoint) => LatLng.fromJson(rawPoint))
323341
?.toList(),
324342
);
325343
}) ??
@@ -342,7 +360,7 @@ Set<Polygon> _rawOptionsToInitialPolygons(Map<String, dynamic> rawOptions) {
342360
visible: rawPolygon['visible'],
343361
zIndex: rawPolygon['zIndex'],
344362
points: rawPolygon['points']
345-
?.map((rawPoint) => LatLng.fromJson(rawPoint))
363+
?.map<LatLng>((rawPoint) => LatLng.fromJson(rawPoint))
346364
?.toList(),
347365
);
348366
}) ??
@@ -418,11 +436,11 @@ gmaps.MarkerOptions _markerOptionsFromMarker(
418436

419437
gmaps.CircleOptions _circleOptionsFromCircle(Circle circle) {
420438
final populationOptions = gmaps.CircleOptions()
421-
..strokeColor = '#' + circle.strokeColor.value.toRadixString(16)
422-
..strokeOpacity = 0.8
439+
..strokeColor = _getCssColor(circle.strokeColor)
440+
..strokeOpacity = _getCssOpacity(circle.strokeColor)
423441
..strokeWeight = circle.strokeWidth
424-
..fillColor = '#' + circle.fillColor.value.toRadixString(16)
425-
..fillOpacity = 0.6
442+
..fillColor = _getCssColor(circle.fillColor)
443+
..fillOpacity = _getCssOpacity(circle.fillColor)
426444
..center = gmaps.LatLng(circle.center.latitude, circle.center.longitude)
427445
..radius = circle.radius
428446
..visible = circle.visible;
@@ -437,11 +455,11 @@ gmaps.PolygonOptions _polygonOptionsFromPolygon(
437455
});
438456
return gmaps.PolygonOptions()
439457
..paths = paths
440-
..strokeColor = '#' + polygon.strokeColor.value.toRadixString(16)
441-
..strokeOpacity = 0.8
458+
..strokeColor = _getCssColor(polygon.strokeColor)
459+
..strokeOpacity = _getCssOpacity(polygon.strokeColor)
442460
..strokeWeight = polygon.strokeWidth
443-
..fillColor = '#' + polygon.fillColor.value.toRadixString(16)
444-
..fillOpacity = 0.35
461+
..fillColor = _getCssColor(polygon.fillColor)
462+
..fillOpacity = _getCssOpacity(polygon.fillColor)
445463
..visible = polygon.visible
446464
..zIndex = polygon.zIndex
447465
..geodesic = polygon.geodesic;
@@ -456,9 +474,9 @@ gmaps.PolylineOptions _polylineOptionsFromPolyline(
456474

457475
return gmaps.PolylineOptions()
458476
..path = paths
459-
..strokeOpacity = 1.0
460477
..strokeWeight = polyline.width
461-
..strokeColor = '#' + polyline.color.value.toRadixString(16).substring(0, 6)
478+
..strokeColor = _getCssColor(polyline.color)
479+
..strokeOpacity = _getCssOpacity(polyline.color)
462480
..visible = polyline.visible
463481
..zIndex = polyline.zIndex
464482
..geodesic = polyline.geodesic;

packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: google_maps_flutter_web
22
description: Web platform implementation of google_maps_flutter
33
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter
4-
version: 0.1.0+2
4+
version: 0.1.0+3
55

66
flutter:
77
plugin:

packages/google_maps_flutter/google_maps_flutter_web/test/test_driver/google_maps_controller_integration.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,26 @@ void main() {
149149
{'markerId': 'marker-1'}
150150
],
151151
'polygonsToAdd': [
152-
{'polygonId': 'polygon-1'}
152+
{
153+
'polygonId': 'polygon-1',
154+
'points': [
155+
[43.355114, -5.851333],
156+
[43.354797, -5.851860],
157+
[43.354469, -5.851318],
158+
[43.354762, -5.850824],
159+
],
160+
},
153161
],
154162
'polylinesToAdd': [
155-
{'polylineId': 'polyline-1'}
163+
{
164+
'polylineId': 'polyline-1',
165+
'points': [
166+
[43.355114, -5.851333],
167+
[43.354797, -5.851860],
168+
[43.354469, -5.851318],
169+
[43.354762, -5.850824],
170+
],
171+
},
156172
],
157173
});
158174
controller.debugSetOverrides(

packages/google_maps_flutter/google_maps_flutter_web/test/test_driver/shapes_integration.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:ui';
67

78
import 'package:integration_test/integration_test.dart';
89
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
910
import 'package:google_maps_flutter_web/google_maps_flutter_web.dart';
1011
import 'package:flutter_test/flutter_test.dart';
1112

13+
// This value is used when comparing the results of
14+
// converting from a byte value to a double between 0 and 1.
15+
// (For Color opacity values, for example)
16+
const _acceptableDelta = 0.01;
17+
1218
/// Test Shapes (Circle, Polygon, Polyline)
1319
void main() {
1420
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
@@ -77,6 +83,25 @@ void main() {
7783
expect(controller.circles, contains(CircleId('2')));
7884
expect(controller.circles, isNot(contains(CircleId('3'))));
7985
});
86+
87+
testWidgets('Converts colors to CSS', (WidgetTester tester) async {
88+
final circles = {
89+
Circle(
90+
circleId: CircleId('1'),
91+
fillColor: Color(0x7FFABADA),
92+
strokeColor: Color(0xFFC0FFEE),
93+
),
94+
};
95+
96+
controller.addCircles(circles);
97+
98+
final circle = controller.circles.values.first.circle;
99+
100+
expect(circle.get('fillColor'), '#fabada');
101+
expect(circle.get('fillOpacity'), closeTo(0.5, _acceptableDelta));
102+
expect(circle.get('strokeColor'), '#c0ffee');
103+
expect(circle.get('strokeOpacity'), closeTo(1, _acceptableDelta));
104+
});
80105
});
81106

82107
group('PolygonsController', () {
@@ -144,6 +169,25 @@ void main() {
144169
expect(controller.polygons, contains(PolygonId('2')));
145170
expect(controller.polygons, isNot(contains(PolygonId('3'))));
146171
});
172+
173+
testWidgets('Converts colors to CSS', (WidgetTester tester) async {
174+
final polygons = {
175+
Polygon(
176+
polygonId: PolygonId('1'),
177+
fillColor: Color(0x7FFABADA),
178+
strokeColor: Color(0xFFC0FFEE),
179+
),
180+
};
181+
182+
controller.addPolygons(polygons);
183+
184+
final polygon = controller.polygons.values.first.polygon;
185+
186+
expect(polygon.get('fillColor'), '#fabada');
187+
expect(polygon.get('fillOpacity'), closeTo(0.5, _acceptableDelta));
188+
expect(polygon.get('strokeColor'), '#c0ffee');
189+
expect(polygon.get('strokeOpacity'), closeTo(1, _acceptableDelta));
190+
});
147191
});
148192

149193
group('PolylinesController', () {
@@ -210,5 +254,21 @@ void main() {
210254
expect(controller.lines, contains(PolylineId('2')));
211255
expect(controller.lines, isNot(contains(PolylineId('3'))));
212256
});
257+
258+
testWidgets('Converts colors to CSS', (WidgetTester tester) async {
259+
final lines = {
260+
Polyline(
261+
polylineId: PolylineId('1'),
262+
color: Color(0x7FFABADA),
263+
),
264+
};
265+
266+
controller.addPolylines(lines);
267+
268+
final line = controller.lines.values.first.line;
269+
270+
expect(line.get('strokeColor'), '#fabada');
271+
expect(line.get('strokeOpacity'), closeTo(0.5, _acceptableDelta));
272+
});
213273
});
214274
}

0 commit comments

Comments
 (0)