I have a CustomPainter
like the one in the example. Where I don't always need to repaint, but I need to keep the previously drawn path because it is used in hitTest
. It is also not a solution to create the path
in the constructor because the Size obtained in the paint
method is used to create the path
.
In this example, progress
can go from 0.0 to 1.0, but many times it stays at 1.0, in this case, it does not need to be repaint but the hitTest needs the path to know if the tap event is inside the path
, in this case where progress
is always 1.0, the shouldRepaint
is false
then path
would be null as long as progress
does not change, which generates that the hitTest is not detected correctly. I don't think always repaint is a good idea since the path would never change when progress
is 1.0 always, but if it`s the only option I won't object.
class CoolPainter extends CustomPainter {
final double progress;
CoolPainter({required this.progress});
static Path _drawRectHole(Size size) {
return Path()
//Cool Drawing stuff.
..close();
}
@override
void paint(Canvas canvas, Size size) {
path = _drawRectHole(size)
canvas.drawPath(
path!,
Paint()
..style = PaintingStyle.fill,
);
}
@override
bool hitTest(Offset position) {
return path!.contains(position);
}
@override
bool shouldRepaint(CoolPainter oldDelegate) {
return oldDelegate.progress != progress;
}
}
-- edit --
The main problem is that when shouldRepait is false then path is null, which makes errors in hitTest. progress
is obtained is obtained from 2 different Animation objects, depending on the case handled outside of the painter