BMI Calculator in Flutter – Part 5 – Animated Slider Hint
Hi again! It’s about time to add some more code to the BMI Calculator app – by Johny Vino’s design. In this post, I will start creating bottom Pacman slider (see below). I will go through implementing the animated hint which will indicate user on how to use the slider. Let’s make it happen! ?
As always, let me show you the effect we want to achieve first. This time I am going to show you the exact result widget from this development instead of the design:
. . .
Static widgets
First, let’s create static aspects of this view, PacmanIcon
and Dot
are simple stateless widgets to be displayed. Since we are going to have 2 sizes of dots, the size will be parametrized:
We are going to place those widgets inside rows and paddings:
The outer Container
specifies slider’s height and decoration (with shape and color) – nothing interesting here.
The main body of the widget is a Row
with pacman and dots. To create the middle dots, we used List.generate
– it allows us to generate any list (in our case a list of widgets) using simple generator (_generateDot
) method. We can also add the bigger dot at the end of the list. Notice that we already specified Opacity
for that dot, the middle ones will be animated later on. Let’s take a sneak peek on what we have:
. . .
AnimationController
Now let’s add the animation controller, which will be responsible for changing dots’ opacity. For now, we can assume that this controller’s animation will take 1 second and after another 800 milliseconds, it will run again from scratch. At this point, we can also wrap middle dots with AnimatedBuilder
, so that they will be rebuilt alongside animation changes. Let’s take a look at the code:
At this point it looks like this:
. . .
Intervals
As you can see, at this point every dot is animated at the same time. I guess it’s a bad hint to swipe right. Theoretically, we could create a list of AnimationControllers
and have them be delayed from each other by a small amount of time, but I don’t think it would be an optimal solution. The approach I went with is the same I used for Flight Search challenge – Intervals
. Intervals are a special kind of animation curves that allow us to “use” only a part of AnimationController. We can specify at what point of our parent animation we want interval animation to begin and end.
Intervals concept
Let’s take a look at how it looks in code:
Notice that we also specified Twin
in which we defined the minimum and maximum values for the animations. Now we can see that dots appear one after another:
. . .
Sinusoidal animation
We are very close to the final effect, now we have to deal with the following problem: After a dot is animated it stays in the maximum state but it should get back to the minimum. Unfortunately, Tween
animations will always give us a curve from begin
to end
and we need it to go from min
to max
to min
again. How to deal with that problem? Create our own Animation!
This animation will provide sinusoidal behavior of the value so that every dot will nicely get from minOpacity
to maxOpacity
and then return back to minOpacity
. The only thing left now is replacing Tween with our new animation and tweaking up the parameters to have the look we want:
And that’s it!
If there is anything unclear or there is something I did wrong, please leave a comment and tell me about it!
You can find the full code for this stage on GitHub.
Cheers ?
? BMI Calculator ?
Be sure to check out the other posts in the series!
Marcin Szałek
Founder of Fidev
Flutter enthusiast since Alpha release in 2017. Believes that sharing is caring, which lead him to start a technical blog dedicated fo Flutter in its early days. Loves to see beautiful designs become real apps and is willing to help make it happen. Enjoys sunny beaches far from home.
Join the newsletter!
Join the newsletter to keep track with latest posts and get my special Widget to animate views' entrances without any hassle for FREE!
Check out other posts!
Stripe Checkout in Flutter Web
Flutter Web is getting more mature every day. If you want to accept payments using Stripe Checkout in your Flutter web application, this article is just for you!
Stripe Checkout in mobile Flutter app
Have you ever struggled to integrate card payments into your mobile Flutter app? If so, today is your lucky day! In this post, I present how to use Stripe Checkout in the Flutter app without any hassle!
Interacting with Widgets using Framy
Have you ever developed a widget or a page and you wanted to make sure it works correctly in different scenarios but then it turned out that you can’t just reproduce all the cases you want to cover? Framy may solve such problems!
Hello Marcin!
Thanks for great post!
May I translate this post series into russian, please?
Priv 🙂
Hey Marcin, great article!!
I was wondering about the implementation of the lerp() function.
Isn’t the goal of going from min to max to min achieved with only overriding the transform function like you did?
So my question is about the implementation of lerp(). This is the implementation in Tween class.
T lerp(double t) {
return begin + (end – begin) * t;
}
And you are doing
return min + (max – min) * math.sin(math.pi * t);
Would you mind explaining why is that necessary?
Thanks!
Also you misspelled Tween in the end of the Intervals sections, below the code sample.
Hey Sebastian,
That’s very good question. Especially that you are right!
I didn’t notice that lerp method was only introduced in Tween, not in Animatable itself, so we could (and probably should!) just use transform method instead.
I will update the code and the typo 😀
Thank you 🙂