I’m writing a custom bar-chart and using AnimatedContainer for the vertical bars. I only have one render, and it does not animate. To remedy this, I have set the initial height to zero for all bars and forced a rebuild. This works, but it is a little messy. Is there another way to force the animation without a rebuild?
Relevant code is:
AnimatedContainer /* BAR-CHART-ITEM */ (
duration: const Duration(seconds: 1),
curve: Curves.easeOut,
width: widget.clsBarChart.dBarWidth,
height: widget.isFirst
? 0
: widget.dHeightScrollBarMax *
widget.clsBarChart.lsdHeightFactors[widget.iNdxItem],
2
Answers
You’re right, forcing a rebuild for animation can be inefficient.
Here are a couple of ways to achieve animation without a rebuild in your custom bar chart:
Another option is to use a Stack and animate the visibility of two widgets: one with zero height and the actual bar with its calculated height. You can use a FadeTransition or a SizeTransition to animate the appearance of the desired bar.
You cannot use Animated Container directly to create vertical bar because Animated Container doesn’t initiate animation in first render instead it follow second render to initiate animation.
Instead you can follow the bellow approach for similar effect.
You can use this Animated Bar with this below sample code.