【发布时间】:2021-08-07 16:07:52
【问题描述】:
我正在使用谷歌地图来显示用户位置。我需要一个自定义标记来显示用户位置。用户图像是从 url 获取的。但是我无法获得如图所示形状的标记.我的代码在下面给出。我的代码无法实现所需的背景形状。任何人请帮助我。
Future<void> getMarkerIcon(String imagePath, Size size) async {
final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Radius radius = Radius.circular(size.width / 2);
final Paint tagPaint = Paint()..color = Utils.myColor.primary;
final double tagWidth = 20.0;
final Paint shadowPaint = Paint()..color = Colors.white;
final double shadowWidth = 2.0;
final Paint borderPaint = Paint()..color = Colors.white;
final double borderWidth = 2.0;
final double imageOffset = shadowWidth + borderWidth;
// Add shadow circle
canvas.drawRRect(
RRect.fromRectAndCorners(
Rect.fromLTWH(0.0, 0.0, size.width, 100),
topLeft: radius,
topRight: radius,
bottomLeft: radius,
bottomRight: radius,
),
shadowPaint);
// Add border circle
canvas.drawRRect(
RRect.fromRectAndCorners(
Rect.fromLTWH(
shadowWidth, shadowWidth, size.width - (shadowWidth * 2), 100),
topLeft: radius,
topRight: radius,
bottomLeft: radius,
bottomRight: radius,
),
borderPaint);
// Oval for the image
Rect oval = Rect.fromLTWH(imageOffset, imageOffset,
size.width - (imageOffset * 2), size.height - (imageOffset * 2));
// Add path for oval image
canvas.clipPath(Path()..addOval(oval));
// Add image
ui.Image image = await getImageFromPath(
imagePath); // Alternatively use your own method to get the image
paintImage(canvas: canvas, image: image, rect: oval, fit: BoxFit.fitWidth);
// Convert canvas to image
final ui.Image markerAsImage =
await pictureRecorder.endRecording().toImage(size.width.toInt(), 100);
// Convert image to bytes
final ByteData byteData =
await markerAsImage.toByteData(format: ui.ImageByteFormat.png);
final Uint8List uint8List = byteData.buffer.asUint8List();
setState(() {
markerIcon = BitmapDescriptor.fromBytes(uint8List);
});
}
Future<ui.Image> getImageFromPath(String imagePath) async {
File imageFile = await DefaultCacheManager().getSingleFile(imagePath);
Uint8List imageBytes = imageFile.readAsBytesSync();
final Completer<ui.Image> completer = new Completer();
ui.decodeImageFromList(imageBytes, (ui.Image img) {
return completer.complete(img);
});
return completer.future;
}
【问题讨论】:
标签: flutter google-maps flutter-layout