Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 8009a2b

Browse files
author
Tim Sneath
authored
Uncomment Marker icons now that ImageListener API change has landed in stable (#2443)
1 parent c13218f commit 8009a2b

File tree

1 file changed

+43
-52
lines changed

1 file changed

+43
-52
lines changed

packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import 'dart:async';
88
import 'dart:math';
99
import 'dart:ui';
10+
import 'dart:typed_data';
1011

1112
import 'package:flutter/material.dart';
1213
import 'package:google_maps_flutter/google_maps_flutter.dart';
@@ -245,42 +246,36 @@ class PlaceMarkerBodyState extends State<PlaceMarkerBody> {
245246
});
246247
}
247248

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+
}
284279

285280
@override
286281
Widget build(BuildContext context) {
@@ -386,22 +381,18 @@ class PlaceMarkerBodyState extends State<PlaceMarkerBody> {
386381
? null
387382
: () => _changeZIndex(selectedId),
388383
),
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+
),
405396
],
406397
),
407398
],

0 commit comments

Comments
 (0)