我有一个CustomPainter,它具有一个更复杂的绘画工具(alot文本)和一个更轻量级的foregroundpainter,可以绘制一个小的突出显示框。
每当突出显示发生变化并调用notifyListeners
时,两者都会重新绘制。
为什么会这样,如何避免呢? 我还尝试了带有两个CustomPaint的Stack,结果相同。
class NotationPlayerWidget extends StatefulWidget {
@override
NotationPlayerState createState() => NotationPlayerState();
}
class NotationPlayerState extends StatefulWidget<NotationPlayerWidget> {
NotationPainter _notationPainter;
HighlightPainter _highlightPainter;
ScrollController _scrollController = ScrollController();
update(Offset pos) {
_highlightPainter.offset = offset;
_highlightPainter.notifyListeners();
}
@override
Widget build(BuildContext context) {
if(_notationPainter == null) _notationPainter = NotationPainter();
if(_highlightPainter == null) _highlightPainter = HighlightPainter();
return SingleChildScrollView(
controller: _scrollController,
child: FittedBox(
fit: BoxFit.fitWidth,
child: CustomPaint(size: CanvasUtil.calcSize(song),
painter: _notationPainter,
foregroundPainter: _highlightPainter,
),
)
);
}
}
class HighlightPainter extends CustomPainter with ChangeNotifier{
HighlightPainter() ;
Offset offset = Offset(0,0);
@override
void paint(Canvas canvas, Size size) {
print("repaint highlight");
//paint stuff
}
@override
bool shouldRepaint(HighlightPainter old) {
false;
}
}
class NotationPainter extends CustomPainter with ChangeNotifier{
NotationPainter() ;
@override
void paint(Canvas canvas, Size size) {
print("repaint notation");
//paint stuff
}
@override
bool shouldRepaint(HighlightPainter old) {
false;
}
}