简体   繁体   English

带有 CustomPaint 的 Flutter InteractiveViewer

[英]Flutter InteractiveViewer with CustomPaint

When scaling and moving with the InteractiveViewer the paint method inside CustomPaint is triggered.当使用InteractiveViewer缩放和移动时,会触发CustomPaintpaint方法。 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;
  }
}

Try to contain your CustomPaint in RepaintBoundary尝试在 RepaintBoundary 中包含您的 CustomPaint

For example例如

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,
    ),
  ),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM