Skip to content

Commit 71404f1

Browse files
Web package html element refactoring
1 parent 972ff34 commit 71404f1

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
@@ -333,22 +333,23 @@ gmaps.Size? _gmSizeFromIconConfig(List<Object?> iconConfig, int sizeIndex) {
333333

334334
// Sets the size and style of the [icon] element.
335335
void _setIconStyle({
336-
required web.Element icon,
336+
required web.HTMLImageElement icon,
337337
required gmaps.Size? size,
338338
required double? opacity,
339339
required bool? isVisible,
340340
}) {
341-
icon.setAttribute(
342-
'style',
343-
<String>[
344-
if (size != null) ...<String>[
345-
'width: ${size.width.toStringAsFixed(1)}px;',
346-
'height: ${size.height.toStringAsFixed(1)}px;',
347-
],
348-
if (opacity != null) 'opacity: $opacity;',
349-
if (isVisible != null) 'visibility: ${isVisible ? 'visible' : 'hidden'};',
350-
].join(' '),
351-
);
341+
final web.CSSStyleDeclaration iconStyle = icon.style;
342+
if (size != null) {
343+
iconStyle
344+
..width = '${size.width.toStringAsFixed(1)}px'
345+
..height = '${size.height.toStringAsFixed(1)}px';
346+
}
347+
if (opacity != null) {
348+
iconStyle.opacity = opacity.toString();
349+
}
350+
if (isVisible != null) {
351+
iconStyle.visibility = isVisible ? 'visible' : 'hidden';
352+
}
352353
}
353354

354355
void _setIconAnchor({
@@ -473,13 +474,10 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
473474
case final CircleGlyph circleGlyph:
474475
options.glyphColor = _getCssColor(circleGlyph.color);
475476
case final TextGlyph textGlyph:
476-
final web.Element element = web.document.createElement('p');
477+
final web.HTMLParagraphElement element = web.HTMLParagraphElement();
477478
element.text = textGlyph.text;
478479
if (textGlyph.textColor != null) {
479-
element.setAttribute(
480-
'style',
481-
'color: ${_getCssColor(textGlyph.textColor!)}',
482-
);
480+
element.style.color = _getCssColor(textGlyph.textColor!);
483481
}
484482
options.glyph = element;
485483
case final BitmapGlyph bitmapGlyph:
@@ -523,8 +521,7 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
523521
_ => throw UnimplementedError(),
524522
};
525523

526-
final web.Element icon = web.document.createElement('img')
527-
..setAttribute('src', url);
524+
final web.HTMLImageElement icon = web.HTMLImageElement()..src = url;
528525

529526
final gmaps.Size? size = switch (bitmapDescriptor.bitmapScaling) {
530527
MapBitmapScaling.auto => await _getBitmapSize(bitmapDescriptor, url),
@@ -547,10 +544,9 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
547544
assert(iconConfig.length >= 2);
548545
// iconConfig[2] contains the DPIs of the screen, but that information is
549546
// already encoded in the iconConfig[1]
550-
final web.Element icon = web.document.createElement('img')..setAttribute(
551-
'src',
552-
ui_web.assetManager.getAssetUrl(iconConfig[1]! as String),
553-
);
547+
final web.HTMLImageElement icon =
548+
web.HTMLImageElement()
549+
..src = ui_web.assetManager.getAssetUrl(iconConfig[1]! as String);
554550

555551
final gmaps.Size? size = _gmSizeFromIconConfig(iconConfig, 3);
556552
_setIconStyle(
@@ -575,8 +571,8 @@ Future<web.Node?> _advancedMarkerIconFromBitmapDescriptor(
575571
// See https://github.com/dart-lang/web/issues/180
576572
blob = web.Blob(<JSUint8Array>[(bytes as Uint8List).toJS].toJS);
577573

578-
final web.Element icon = web.document.createElement('img')
579-
..setAttribute('src', web.URL.createObjectURL(blob as JSObject));
574+
final web.HTMLImageElement icon =
575+
web.HTMLImageElement()..src = web.URL.createObjectURL(blob as JSObject);
580576

581577
final gmaps.Size? size = _gmSizeFromIconConfig(iconConfig, 2);
582578
_setIconStyle(

0 commit comments

Comments
 (0)