Skip to content

Commit f30050c

Browse files
Web package html element refactoring
1 parent 770f281 commit f30050c

File tree

1 file changed

+21
-25
lines changed
  • packages/google_maps_flutter/google_maps_flutter_web/lib/src

1 file changed

+21
-25
lines changed

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,23 @@ gmaps.Size? _gmSizeFromIconConfig(List<Object?> iconConfig, int sizeIndex) {
336336

337337
// Sets the size and style of the [icon] element.
338338
void _setIconStyle({
339-
required web.Element icon,
339+
required web.HTMLImageElement icon,
340340
required gmaps.Size? size,
341341
required double? opacity,
342342
required bool? isVisible,
343343
}) {
344-
icon.setAttribute(
345-
'style',
346-
<String>[
347-
if (size != null) ...<String>[
348-
'width: ${size.width.toStringAsFixed(1)}px;',
349-
'height: ${size.height.toStringAsFixed(1)}px;',
350-
],
351-
if (opacity != null) 'opacity: $opacity;',
352-
if (isVisible != null) 'visibility: ${isVisible ? 'visible' : 'hidden'};',
353-
].join(' '),
354-
);
344+
final web.CSSStyleDeclaration iconStyle = icon.style;
345+
if (size != null) {
346+
iconStyle
347+
..width = '${size.width.toStringAsFixed(1)}px'
348+
..height = '${size.height.toStringAsFixed(1)}px';
349+
}
350+
if (opacity != null) {
351+
iconStyle.opacity = opacity.toString();
352+
}
353+
if (isVisible != null) {
354+
iconStyle.visibility = isVisible ? 'visible' : 'hidden';
355+
}
355356
}
356357

357358
void _setIconAnchor({
@@ -476,13 +477,10 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
476477
case final CircleGlyph circleGlyph:
477478
options.glyphColor = _getCssColor(circleGlyph.color);
478479
case final TextGlyph textGlyph:
479-
final web.Element element = web.document.createElement('p');
480+
final web.HTMLParagraphElement element = web.HTMLParagraphElement();
480481
element.text = textGlyph.text;
481482
if (textGlyph.textColor != null) {
482-
element.setAttribute(
483-
'style',
484-
'color: ${_getCssColor(textGlyph.textColor!)}',
485-
);
483+
element.style.color = _getCssColor(textGlyph.textColor!);
486484
}
487485
options.glyph = element;
488486
case final BitmapGlyph bitmapGlyph:
@@ -526,8 +524,7 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
526524
_ => throw UnimplementedError(),
527525
};
528526

529-
final web.Element icon = web.document.createElement('img')
530-
..setAttribute('src', url);
527+
final web.HTMLImageElement icon = web.HTMLImageElement()..src = url;
531528

532529
final gmaps.Size? size = switch (bitmapDescriptor.bitmapScaling) {
533530
MapBitmapScaling.auto => await _getBitmapSize(bitmapDescriptor, url),
@@ -550,10 +547,9 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
550547
assert(iconConfig.length >= 2);
551548
// iconConfig[2] contains the DPIs of the screen, but that information is
552549
// already encoded in the iconConfig[1]
553-
final web.Element icon = web.document.createElement('img')..setAttribute(
554-
'src',
555-
ui_web.assetManager.getAssetUrl(iconConfig[1]! as String),
556-
);
550+
final web.HTMLImageElement icon =
551+
web.HTMLImageElement()
552+
..src = ui_web.assetManager.getAssetUrl(iconConfig[1]! as String);
557553

558554
final gmaps.Size? size = _gmSizeFromIconConfig(iconConfig, 3);
559555
_setIconStyle(
@@ -578,8 +574,8 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
578574
// See https://github.com/dart-lang/web/issues/180
579575
blob = web.Blob(<JSUint8Array>[(bytes as Uint8List).toJS].toJS);
580576

581-
final web.Element icon = web.document.createElement('img')
582-
..setAttribute('src', web.URL.createObjectURL(blob as JSObject));
577+
final web.HTMLImageElement icon =
578+
web.HTMLImageElement()..src = web.URL.createObjectURL(blob as JSObject);
583579

584580
final gmaps.Size? size = _gmSizeFromIconConfig(iconConfig, 2);
585581
_setIconStyle(

0 commit comments

Comments
 (0)