|
7 | 7 | import 'dart:async'; |
8 | 8 | import 'dart:math'; |
9 | 9 | import 'dart:ui'; |
| 10 | +import 'dart:typed_data'; |
10 | 11 |
|
11 | 12 | import 'package:flutter/material.dart'; |
12 | 13 | import 'package:google_maps_flutter/google_maps_flutter.dart'; |
@@ -245,42 +246,36 @@ class PlaceMarkerBodyState extends State<PlaceMarkerBody> { |
245 | 246 | }); |
246 | 247 | } |
247 | 248 |
|
248 | | -// A breaking change to the ImageStreamListener API affects this sample. |
249 | | -// I've updates the sample to use the new API, but as we cannot use the new |
250 | | -// API before it makes it to stable I'm commenting out this sample for now |
251 | | -// TODO(amirh): uncomment this one the ImageStream API change makes it to stable. |
252 | | -// https://github.com/flutter/flutter/issues/33438 |
253 | | -// |
254 | | -// void _setMarkerIcon(BitmapDescriptor assetIcon) { |
255 | | -// if (selectedMarker == null) { |
256 | | -// return; |
257 | | -// } |
258 | | -// |
259 | | -// final Marker marker = markers[selectedMarker]; |
260 | | -// setState(() { |
261 | | -// markers[selectedMarker] = marker.copyWith( |
262 | | -// iconParam: assetIcon, |
263 | | -// ); |
264 | | -// }); |
265 | | -// } |
266 | | -// |
267 | | -// Future<BitmapDescriptor> _getAssetIcon(BuildContext context) async { |
268 | | -// final Completer<BitmapDescriptor> bitmapIcon = |
269 | | -// Completer<BitmapDescriptor>(); |
270 | | -// final ImageConfiguration config = createLocalImageConfiguration(context); |
271 | | -// |
272 | | -// const AssetImage('assets/red_square.png') |
273 | | -// .resolve(config) |
274 | | -// .addListener(ImageStreamListener((ImageInfo image, bool sync) async { |
275 | | -// final ByteData bytes = |
276 | | -// await image.image.toByteData(format: ImageByteFormat.png); |
277 | | -// final BitmapDescriptor bitmap = |
278 | | -// BitmapDescriptor.fromBytes(bytes.buffer.asUint8List()); |
279 | | -// bitmapIcon.complete(bitmap); |
280 | | -// })); |
281 | | -// |
282 | | -// return await bitmapIcon.future; |
283 | | -// } |
| 249 | + void _setMarkerIcon(MarkerId markerId, BitmapDescriptor assetIcon) { |
| 250 | + final Marker marker = markers[markerId]!; |
| 251 | + setState(() { |
| 252 | + markers[markerId] = marker.copyWith( |
| 253 | + iconParam: assetIcon, |
| 254 | + ); |
| 255 | + }); |
| 256 | + } |
| 257 | + |
| 258 | + Future<BitmapDescriptor> _getAssetIcon(BuildContext context) async { |
| 259 | + final Completer<BitmapDescriptor> bitmapIcon = |
| 260 | + Completer<BitmapDescriptor>(); |
| 261 | + final ImageConfiguration config = createLocalImageConfiguration(context); |
| 262 | + |
| 263 | + const AssetImage('assets/red_square.png') |
| 264 | + .resolve(config) |
| 265 | + .addListener(ImageStreamListener((ImageInfo image, bool sync) async { |
| 266 | + final ByteData? bytes = |
| 267 | + await image.image.toByteData(format: ImageByteFormat.png); |
| 268 | + if (bytes == null) { |
| 269 | + bitmapIcon.completeError(Exception('Unable to encode icon')); |
| 270 | + return; |
| 271 | + } |
| 272 | + final BitmapDescriptor bitmap = |
| 273 | + BitmapDescriptor.fromBytes(bytes.buffer.asUint8List()); |
| 274 | + bitmapIcon.complete(bitmap); |
| 275 | + })); |
| 276 | + |
| 277 | + return await bitmapIcon.future; |
| 278 | + } |
284 | 279 |
|
285 | 280 | @override |
286 | 281 | Widget build(BuildContext context) { |
@@ -386,22 +381,18 @@ class PlaceMarkerBodyState extends State<PlaceMarkerBody> { |
386 | 381 | ? null |
387 | 382 | : () => _changeZIndex(selectedId), |
388 | 383 | ), |
389 | | - // A breaking change to the ImageStreamListener API affects this sample. |
390 | | - // I've updates the sample to use the new API, but as we cannot use the new |
391 | | - // API before it makes it to stable I'm commenting out this sample for now |
392 | | - // TODO(amirh): uncomment this one the ImageStream API change makes it to stable. |
393 | | - // https://github.com/flutter/flutter/issues/33438 |
394 | | - // |
395 | | - // TextButton( |
396 | | - // child: const Text('set marker icon'), |
397 | | - // onPressed: () { |
398 | | - // _getAssetIcon(context).then( |
399 | | - // (BitmapDescriptor icon) { |
400 | | - // _setMarkerIcon(icon); |
401 | | - // }, |
402 | | - // ); |
403 | | - // }, |
404 | | - // ), |
| 384 | + TextButton( |
| 385 | + child: const Text('set marker icon'), |
| 386 | + onPressed: selectedId == null |
| 387 | + ? null |
| 388 | + : () { |
| 389 | + _getAssetIcon(context).then( |
| 390 | + (BitmapDescriptor icon) { |
| 391 | + _setMarkerIcon(selectedId, icon); |
| 392 | + }, |
| 393 | + ); |
| 394 | + }, |
| 395 | + ), |
405 | 396 | ], |
406 | 397 | ), |
407 | 398 | ], |
|
0 commit comments