From d8d9d44db6c08c8fa1cbfd4f7aed67741d8bf6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kowali=C5=84ski?= Date: Sat, 3 Aug 2024 17:10:37 +0200 Subject: [PATCH] fix(map): normalize markers size --- lib/config/map_view_config.dart | 6 +- .../map_view/utils/map_marker_utils.dart | 60 ++++++++----------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/lib/config/map_view_config.dart b/lib/config/map_view_config.dart index ab6d24b7..573e8959 100644 --- a/lib/config/map_view_config.dart +++ b/lib/config/map_view_config.dart @@ -36,8 +36,10 @@ abstract class MapWidgetConfig { ); static const mapType = MapType.normal; - static const mapMarkerOriginWidth = 28; - static const activeMapMarkerOriginWidth = 40; + static const mapMarkerWidth = 22.0; + static const mapMarkerHeight = 34.0; + static const activeMapMarkerWidth = 38.0; + static const activeMapMarkerHeight = 58.0; } abstract class BuildingSearchConfig { diff --git a/lib/features/map_view/utils/map_marker_utils.dart b/lib/features/map_view/utils/map_marker_utils.dart index 1c546f86..e15e1914 100644 --- a/lib/features/map_view/utils/map_marker_utils.dart +++ b/lib/features/map_view/utils/map_marker_utils.dart @@ -1,7 +1,4 @@ -import "dart:ui" as ui; - import "package:flutter/foundation.dart"; -import "package:flutter/services.dart"; import "package:flutter/widgets.dart"; import "package:google_maps_flutter/google_maps_flutter.dart"; @@ -15,47 +12,42 @@ class MapMarkerUtils { static late final BitmapDescriptor activeParkingMapMarker; static Future loadMapMarkerAssets(BuildContext context) async { - final pixelRatio = - kIsWeb ? 1 : MediaQuery.devicePixelRatioOf(context).toInt(); - - buildingMapMarker = await _AssetScaledLoader.loadScaledBitmapDescriptor( + final configuration = context.mapMarkerConfiguration; + final activeMarkerConfig = context.activeMapMarkerConfiguration; + buildingMapMarker = await BitmapDescriptor.asset( + configuration, Assets.mapMarkers.mapMarker.path, - width: MapWidgetConfig.mapMarkerOriginWidth * pixelRatio, ); - - activeBuildingMapMarker = - await _AssetScaledLoader.loadScaledBitmapDescriptor( + activeBuildingMapMarker = await BitmapDescriptor.asset( + activeMarkerConfig, Assets.mapMarkers.activeMapMarker.path, - width: MapWidgetConfig.activeMapMarkerOriginWidth * pixelRatio, ); - parkingMapMarker = await _AssetScaledLoader.loadScaledBitmapDescriptor( + parkingMapMarker = await BitmapDescriptor.asset( + configuration, Assets.mapMarkers.parkingMapMarker.path, - width: MapWidgetConfig.mapMarkerOriginWidth * pixelRatio, ); - activeParkingMapMarker = - await _AssetScaledLoader.loadScaledBitmapDescriptor( + activeParkingMapMarker = await BitmapDescriptor.asset( + activeMarkerConfig, Assets.mapMarkers.activeParkingMapMarker.path, - width: MapWidgetConfig.activeMapMarkerOriginWidth * pixelRatio, ); } } -class _AssetScaledLoader { - static Future loadScaledBitmapDescriptor( - String assetName, { - required int width, - }) async { - try { - final data = await rootBundle.load(assetName); - final codec = await ui.instantiateImageCodec( - data.buffer.asUint8List(), - targetWidth: width, +extension _ImageConfigurationX on BuildContext { + double get pixelRatio => kIsWeb ? 1.0 : MediaQuery.devicePixelRatioOf(this); + + ImageConfiguration get mapMarkerConfiguration => ImageConfiguration( + devicePixelRatio: pixelRatio, + size: const Size( + MapWidgetConfig.mapMarkerWidth, + MapWidgetConfig.mapMarkerHeight, + ), + ); + ImageConfiguration get activeMapMarkerConfiguration => ImageConfiguration( + devicePixelRatio: pixelRatio, + size: const Size( + MapWidgetConfig.activeMapMarkerWidth, + MapWidgetConfig.activeMapMarkerHeight, + ), ); - final fi = await codec.getNextFrame(); - final decode = await fi.image.toByteData(format: ui.ImageByteFormat.png); - return BitmapDescriptor.bytes(decode!.buffer.asUint8List()); - } on Exception { - return BitmapDescriptor.defaultMarker; // fallback - } - } }