How can I make a multicolor custom progress bar with animation? I have three values that determine the length of each color. Animation trigger when app started. I’ve tried make it with SizeAnimation just to expand colored container but it was a bad way.
I try to make it with CustomPainter, but I do something wrong
class MultiColorsLinePainter extends CustomPainter {
const MultiColorsLinePainter({required this.point});
final double point;
@override
void paint(Canvas canvas, Size size) {
final activePaint = Paint()
..color = Colors.teal
..strokeWidth = 11
..strokeCap = StrokeCap.round;
final frozenPaint = Paint()
..color = Colors.blue
..strokeWidth = 11
..strokeCap = StrokeCap.butt;
final sellPaint = Paint()
..color = Colors.red
..strokeWidth = 11
..strokeCap = StrokeCap.round;
final activeStartingPoint = Offset(0, size.height / 2);
final activeEndingPoint = Offset(size.width * point, size.height / 2);
final frozenStartingPoint = Offset(activeEndingPoint.dx, size.height / 2);
final frozenEndingPoint = Offset(activeEndingPoint.dx * point, size.height / 2);
final sellStartingPoint = Offset(frozenEndingPoint.dx, size.height / 2);
final sellEndingPoint = Offset(frozenEndingPoint.dx * point, size.height / 2);
canvas
..drawLine(activeStartingPoint, activeEndingPoint, activePaint)
..drawLine(frozenStartingPoint, frozenEndingPoint, frozenPaint)
..drawLine(sellStartingPoint, sellEndingPoint, sellPaint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) =>
oldDelegate.paint != paint;
}
2
Answers
I made it myself with CustomPainter
There are two ways to create a nice ani animation here. One option is to have a Row with 3 sized boxes and 3 animation controllers firing in sequence.
A better option may be to have a Row with 3 decorated containers each wrapped in Expanded widget. Each Expanded widget has its flex property set to relative width. Then you have another container in this row which has specific width. At first, you set its width to be as wide as the progress bar. Then you have an animation controller that changes its width down to zero. As this container gets smaller, the other 3 containers will expand to take all the space. It will look cool.