带有CustomPaint的Flutter InteractiveViewer

时间:2020-09-21 05:44:03

标签: flutter

使用InteractiveViewer缩放和移动时,将触发paint内部的CustomPaint方法。如何预防呢?

...
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;
  }
}

1 个答案:

答案 0 :(得分:0)

尝试在 RepaintBoundary 中包含您的 CustomPaint

例如

final List<Widget> desks = _mapDataState.mapObjects.desks
    .map((desk) => RepaintBoundary(
          child: CustomPaint(
            size: Size.infinite,
            painter: DeskPainter(desk),
          ),
        ))
    .toList();

return InteractiveViewer(
  maxScale: 6,
  minScale: 0.3,
  child: CustomPaint(
    size: Size.infinite,
    painter: MapPainter(snapshot.data),
    child: Stack(
      children: desks,
    ),
  ),
);