Skip to content

Commit 90d5ec6

Browse files
committed
Improve performance by reducing repaints
1 parent c4833a7 commit 90d5ec6

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

lib/chart_painter.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ class ChartPainter extends CustomPainter {
273273
}
274274

275275
@override
276-
bool shouldRepaint(ChartPainter oldDelegate) => true;
276+
bool shouldRepaint(ChartPainter oldDelegate) =>
277+
params.shouldRepaint(oldDelegate.params);
277278
}
278279

279280
extension ElementAtOrNull<E> on List<E> {

lib/interactive_chart.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ class _InteractiveChartState extends State<InteractiveChart> {
141141
duration: Duration(milliseconds: 300),
142142
curve: Curves.easeOut,
143143
builder: (_, PainterParams params, __) {
144-
return CustomPaint(
145-
size: size,
146-
painter: ChartPainter(
147-
params: params,
148-
getTimeLabel: widget.timeLabel ?? defaultTimeLabel,
149-
getPriceLabel: widget.priceLabel ?? defaultPriceLabel,
150-
getOverlayInfo: widget.overlayInfo ?? defaultOverlayInfo,
144+
return RepaintBoundary(
145+
child: CustomPaint(
146+
size: size,
147+
painter: ChartPainter(
148+
params: params,
149+
getTimeLabel: widget.timeLabel ?? defaultTimeLabel,
150+
getPriceLabel: widget.priceLabel ?? defaultPriceLabel,
151+
getOverlayInfo: widget.overlayInfo ?? defaultOverlayInfo,
152+
),
151153
),
152154
);
153155
},

lib/painter_params.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ class PainterParams {
8383
trailingTrend: b.trailingTrend,
8484
);
8585
}
86+
87+
bool shouldRepaint(PainterParams other) {
88+
if (candles.length != other.candles.length) return true;
89+
90+
if (size != other.size ||
91+
candleWidth != other.candleWidth ||
92+
startOffset != other.startOffset ||
93+
xShift != other.xShift) return true;
94+
95+
if (maxPrice != other.maxPrice ||
96+
minPrice != other.minPrice ||
97+
maxVol != other.maxVol ||
98+
minVol != other.minVol) return true;
99+
100+
if (tapPosition != other.tapPosition) return true;
101+
102+
if (leadingTrend != other.leadingTrend ||
103+
trailingTrend != other.trailingTrend) return true;
104+
105+
if (style != other.style) return true;
106+
107+
return false;
108+
}
86109
}
87110

88111
class PainterParamsTween extends Tween<PainterParams> {

0 commit comments

Comments
 (0)