I'm using a bit of code to display locations on a map using Flutter paintImage. Basically, I have a png of a map given to me by my backend, as well as a list of Offset where I want to place a pin image. I added the pin image in my asset folder and wrote the following bit of code:
On my view:
Center(
child: SizedBox(
height: ...,
width: ...,
child: RepaintBoundary(
height: ...,
child: CustomPaint(
paint: myMapPainter(backgroundMap, listPins, width, height),
),
),
),
),
Defining the mapPainter:
class myMapPainter extends CustomPainter {
final backgroundImage;
final listOfPins;
final width;
final double;
@override
Future<void> paint(Canvas canvas, Size size) async {
log("Start painting with list of ${listOfPins.length} map pins.");
paintImage(
canvas: canvas,
rect: Rect.fromLTWH(0, 0, size.width, size.height),
image: backgroundImage,
fit: BoxFit.scaleDown,
filterQuality: FilterQuality.high,);
for (final Offset item in listOfPins) {
log("painting item with x: ${item.dx} y: ${item.dy}");
paintImage(
canvas: canvas,
image: imagePin,
rect:
Rect.fromLTWH(item.dx * width - 16, item.dy * height - 16, 32, 32),
);
}
}
@override
bool shouldRepaint(DamagePainter oldDelegate) => true;
}
As you can see, this should paint the background image, then paint the images of the pin at the coordinates described in the Offset list the backend provides. My problem is that despite the log showing we are painting a number of pings greater than 0 and offsets between 0 and 1 for both coordinates.
This works well most of the time, but sometime it just doesn't. There is no error message, the logs show the pin data is there and it makes sense, but sometime it just does not paint. Any idea why ?
Thanks in advance !