Skip to content

Instantly share code, notes, and snippets.

@stefanJi
Created June 9, 2019 15:01
Show Gist options
  • Save stefanJi/010ca551a3c485e8956696fa992d7d84 to your computer and use it in GitHub Desktop.
Save stefanJi/010ca551a3c485e8956696fa992d7d84 to your computer and use it in GitHub Desktop.
abstract class IntervalProgressPainter extends CustomPainter {
final int max;
final int progress;
final int intervalSize;
final Color highlightColor;
final Color defaultColor;
final Color intervalColor;
final Color intervalHighlightColor;
final double radius;
final bool reverse;
final Paint _paint = Paint()
..style = PaintingStyle.fill
..isAntiAlias = true;
Rect bound;
@override
@mustCallSuper
void paint(Canvas canvas, Size size) {
if (progress > max) {
throw Exception("progress must <= max");
}
bound = Offset.zero & size;
Size blockSize = calBlockSize();
for (int i = 0; i < max; i++) {
paintBlock(canvas, i, blockSize);
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
final old = oldDelegate as IntervalProgressPainter;
return old.max != max ||
old.progress != progress ||
old.intervalSize != intervalSize ||
old.intervalColor != intervalColor ||
old.defaultColor != defaultColor ||
old.highlightColor != highlightColor ||
old.intervalHighlightColor != intervalHighlightColor ||
old.radius != radius;
}
bool highlightBlock(int index) =>
reverse ? index >= (max - progress) : index < progress;
bool highlightInterval(int index) =>
reverse ? index >= (max - progress - 1) : index < progress - 1;
void paintBlock(Canvas canvas, int blockIndex, Size blockSize);
Size calBlockSize();
bool shouldDrawStartRadius(int index) => index == 0 && radius > 0;
bool shouldDrawEndRadius(int index) => index == max - 1 && radius > 0;
bool shouldDrawInterval(int index) =>
index != max - 1 &&
(intervalColor != IntervalProgressBar.TRANSPARENT ||
intervalHighlightColor != IntervalProgressBar.TRANSPARENT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment