flutter

Flutter InteractiveViewer with CustomPaint


When scaling and moving with the InteractiveViewer the paint method inside CustomPaint is triggered. How to prevent that?

...
InteractiveViewer(                  
  child: CustomPaint(
    painter: TestPainter(),
  ),
),
...

class TestPainter extends CustomPainter {

  @override
  void paint(Canvas canvas, Size size) {
    print('painting...');
    
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    
    return false;
  }
}

Solution

  • Try to contain your CustomPaint in RepaintBoundary

    For example

    return InteractiveViewer(
      maxScale: 6,
      minScale: 0.3,
      child: CustomPaint(
        size: Size.infinite,
        painter: MapPainter(snapshot.data),
        child: RepaintBoundary(
              child: CustomPaint(
                size: Size.infinite,
                painter: DeskPainter(desk),
              ),
            ),
      ),
    );