'Round Image File
I did this code to get an image from firestore and use it as an icon for a Map Marker.
final StorageReference storageReference =
FirebaseStorage().ref().child("ProfilePictures/" + widget.userId);
String avatarDownloadPath = await storageReference.getDownloadURL();
final File _avatar = await DefaultCacheManager().getSingleFile(avatarDownloadPath);
Uint8List __avatar = await _avatar.readAsBytes();
BitmapDescriptor avatar = BitmapDescriptor.fromBytes(__avatar);
setState(() {
_markers.add(Marker(markerId: MarkerId("UserPosition"), position: userLocation, icon: avatar ));
});
This code works but I want to set the image a round shape and I don't know how to do this...
If you also know how to add a circle around and animate it like this I will be really satisfied:
(I didn't find something more representative but I just want one circle)
Solution 1:[1]
You can use use Canvas to draw a round image and use PictureRecorder to save as a final image and put it in the marker bitmapdescriptor, fyi: custom marker
Replace TextPainter with ImagePainter in the article
OR
Follow this article by using widget, custom marker
Solution 2:[2]
Thanks to @Jim Chiu, here my implementation:
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
Future<BitmapDescriptor> convertImageFileToCustomBitmapDescriptor(File imageFile,
{int size = 150,
bool addBorder = false,
Color borderColor = Colors.white,
double borderSize = 10,
String title,
Color titleColor = Colors.white,
Color titleBackgroundColor = Colors.black}) async {
final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Paint paint = Paint()..color;
final TextPainter textPainter = TextPainter(
textDirection: TextDirection.ltr,
);
final double radius = size / 2;
//make canvas clip path to prevent image drawing over the circle
final Path clipPath = Path();
clipPath.addRRect(RRect.fromRectAndRadius(
Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
Radius.circular(100)));
clipPath.addRRect(RRect.fromRectAndRadius(
Rect.fromLTWH(0, size * 8 / 10, size.toDouble(), size * 3 / 10),
Radius.circular(100)));
canvas.clipPath(clipPath);
//paintImage
final Uint8List imageUint8List = await imageFile.readAsBytes();
final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List);
final ui.FrameInfo imageFI = await codec.getNextFrame();
paintImage(
canvas: canvas,
rect: Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
image: imageFI.image);
if (addBorder) {
//draw Border
paint..color = borderColor;
paint..style = PaintingStyle.stroke;
paint..strokeWidth = borderSize;
canvas.drawCircle(Offset(radius, radius), radius, paint);
}
if (title != null) {
if (title.length > 9) {
title = title.substring(0, 9);
}
//draw Title background
paint..color = titleBackgroundColor;
paint..style = PaintingStyle.fill;
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, size * 8 / 10, size.toDouble(), size * 3 / 10),
Radius.circular(100)),
paint);
//draw Title
textPainter.text = TextSpan(
text: title,
style: TextStyle(
fontSize: radius / 2.5,
fontWeight: FontWeight.bold,
color: titleColor,
));
textPainter.layout();
textPainter.paint(
canvas,
Offset(radius - textPainter.width / 2,
size * 9.5 / 10 - textPainter.height / 2));
}
//convert canvas as PNG bytes
final _image =
await pictureRecorder.endRecording().toImage(size, (size * 1.1).toInt());
final data = await _image.toByteData(format: ui.ImageByteFormat.png);
//convert PNG bytes as BitmapDescriptor
return BitmapDescriptor.fromBytes(data.buffer.asUint8List());
}
and here the result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |