0

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 !

Tom
  • 163
  • 1
  • 11
  • Paint should not be async (as some warning is probably already pointing out) and if you aren't trying to save the whole thing as an image, you also don't need the repaint boundary. Instead implement `shouldRepaint` correctly, if you want to minimize repaints. – Valiumdiät Aug 11 '23 at 23:29
  • @Valiumdiät changed it to remove async from the `paint` method, didn't fix anything. Could it have to do the canvas is drawn as part of a `FutureBuilder` ? Part of the feature is being able to save the map as an image so that part is intentional. – Tom Aug 17 '23 at 08:01

0 Answers0