我如何对飞镖中的函数进行相等性检查(以在CustomPainter的shouldRepaint方法中使用?)>

问题描述 投票:0回答:1

我对某个功能上的飞镖相等性检查有问题。

我有一个CustomPainter,我想有效地实现它的shouldRepaint()方法。

绘画器中有一个功能字段(ColorResolver),该函数字段给出y值并获得用于绘制线条的颜色,我希望将此逻辑在绘画器之外进行处理。

检查此代码:

typedef ColorResolver = Color Function(double value);

class MyPainter extends CustomPainter {
  final ColorResolver colorResolver;
  MyPainter(this.colorResolver);

  @override
  void paint(Canvas canvas, Size size) {
    for (double y = 0; y <= size.height; y += 10) {
      final paint = Paint()..color = colorResolver(y);
      canvas.drawLine(Offset(0, y), Offset(size.width, y), paint);
    }
  }

  @override
  bool shouldRepaint(MyPainter old) => old.colorResolver == colorResolver;
}

只要提供的ColorResolver逻辑与以前一样,如何防止重新绘制?

我对某个功能的飞镖相等性检查有问题。我有一个CustomPainter,我想有效地实现它的shouldRepaint()方法。 ...

flutter dart equality custom-painting
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.