From 720b76258a267c3672cdc484ba04e5eb61bd69f7 Mon Sep 17 00:00:00 2001 From: David Iglesias Teixeira Date: Thu, 4 Aug 2022 18:38:43 -0700 Subject: [PATCH 1/2] [google_maps_flutter_platform_interface] Adds size to BitmapDescriptor.fromBytes --- .../CHANGELOG.md | 6 +++ .../lib/src/types/bitmap.dart | 18 ++++++- .../pubspec.yaml | 2 +- .../test/types/bitmap_test.dart | 52 +++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md index e8da5f3aba99..5aae75133045 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md @@ -1,3 +1,9 @@ +## 2.2.2 + +* Adds a `size` parameter to `BitmapDescriptor.fromBytes`, so **web** applications + can specify the actual *physical size* of the bitmap. The parameter is not needed + (and ignored) in other platforms. Issue [#73789](https://github.com/flutter/flutter/issues/73789). + ## 2.2.1 * Adds a new interface for inspecting the platform map state in tests. diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart index 0ccc3e624abe..7dda43a7abf4 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart @@ -157,8 +157,22 @@ class BitmapDescriptor { /// Creates a BitmapDescriptor using an array of bytes that must be encoded /// as PNG. - static BitmapDescriptor fromBytes(Uint8List byteData) { - return BitmapDescriptor._([_fromBytes, byteData]); + /// On the web, the [size] parameter represents the *physical size* of the + /// bitmap, regardless of the actual resolution of the encoded PNG. + /// This helps the browser to render High-DPI images at the correct size. + /// `size` is not required (and ignored, if passed) in other platforms. + static BitmapDescriptor fromBytes(Uint8List byteData, {Size? size}) { + assert(byteData.isNotEmpty, + 'Cannot create BitmapDescriptor with empty byteData'); + return BitmapDescriptor._([ + _fromBytes, + byteData, + if (kIsWeb && size != null) + [ + size.width, + size.height, + ] + ]); } final Object _json; diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml index f1463c71230a..9f4b7c03fdb2 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/google_maps_fl issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22 # NOTE: We strongly prefer non-breaking changes, even at the expense of a # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes -version: 2.2.1 +version: 2.2.2 environment: sdk: '>=2.12.0 <3.0.0' diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart index 7fbaf4998355..a6f890ca07a4 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart @@ -3,7 +3,9 @@ // found in the LICENSE file. import 'dart:typed_data'; +import 'dart:ui'; +import 'package:flutter/foundation.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; @@ -26,6 +28,56 @@ void main() { expect(identical(descriptorFromJson.toJson(), json), isTrue); // Same JSON }); + group('fromBytes constructor', () { + test('with empty byte array, throws assertion error', () { + expect(() { + BitmapDescriptor.fromBytes(Uint8List.fromList([])); + }, throwsAssertionError); + }); + + test('with bytes', () { + final BitmapDescriptor descriptor = BitmapDescriptor.fromBytes( + Uint8List.fromList([1, 2, 3]), + ); + expect(descriptor, isA()); + expect( + descriptor.toJson(), + equals([ + 'fromBytes', + [1, 2, 3], + ])); + }); + + test('with size, not on the web, size is ignored', () { + final BitmapDescriptor descriptor = BitmapDescriptor.fromBytes( + Uint8List.fromList([1, 2, 3]), + size: const Size(40, 20), + ); + + expect( + descriptor.toJson(), + equals([ + 'fromBytes', + [1, 2, 3], + ])); + }, skip: kIsWeb); + + test('with size, on the web, size is preserved', () { + final BitmapDescriptor descriptor = BitmapDescriptor.fromBytes( + Uint8List.fromList([1, 2, 3]), + size: const Size(40, 20), + ); + + expect( + descriptor.toJson(), + equals([ + 'fromBytes', + [1, 2, 3], + [40, 20], + ])); + }, skip: !kIsWeb); + }); + group('fromJson validation', () { group('type validation', () { test('correct type', () { From 2d7bdbdce2782f2348338268c8343e3e469ea8fe Mon Sep 17 00:00:00 2001 From: David Iglesias Teixeira Date: Thu, 4 Aug 2022 21:20:53 -0700 Subject: [PATCH 2/2] Ignore unnecessary_import in test --- .../test/types/bitmap_test.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart index a6f890ca07a4..2499e87bb649 100644 --- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// ignore:unnecessary_import import 'dart:typed_data'; import 'dart:ui';