BMI Calculator in Flutter – part 1 – Gender
I guess it’s good to start with some clarifications:
- I have the exact specs for this design so there will be no guessing on what the padding values should be etc. What’s more I have access to the assets so working with icons will be much easier.
- We want to implement more than just the view, our goal is to publish this app to the store. To do that, I have to keep in mind all the possible screen resolutions. Some logic will need to be implemented as well.
- I will split the implementation of this design to multiple posts so that it will be easier to read. In this post, I will cover the setup and gender indicator.
Let’s get to it!
. . .
First of all, it is worth noticing that the design is only meant for vertical orientation. To ensure that, we can either set it Android/iOS settings or (as a quick fix) we can use
As mentioned earlier, we need to keep in mind that we want to support every possible screen size. That means, that doing fixed sizes like
height: 80.0 is not a good solution for us. However, I have design specs with specific margins and sizes for a device with the height of 650dp. In order to keep proportions similar for every device I came up with following util method:
This method uses
MediaQuery.of(context).size which returns the actual device size in logical pixels. It means, we can convert the design value to device value so that proportions will remain the same. I will use this method mostly for vertical values since those are my main concerns for that project. This allows us to deploy on Nexus S as well as Pixel 2XL and have very similar UIs.
EDIT: Actually, we should use MediaQuery.of(context).size.shortestSide which is already known as swDp on Android. It allows using the scaling both in landscape and portrait. Thanks to Simon Lightfoot
. . .
1. Input screen layout
The input screen is the view where the user will input their metrics. I see it as a
Column with three elements: title, cards, and swipe button:
As you can see, we wrapped everything in
MediaQuery.padding so that we are sure we the view will not overlap with platform UI elements (if you are smarter than me, you should use SafeArea widget instead). The title and bottom button will take “fixed” amount of space and then the input cards will take the rest in the middle.
Title widget is pretty simple:
And in the bottom part, we will use a
Switch as a placeholder:
screenAwareSize method to take estimate the actual values of dimensions.
When it comes to building the middle part, it is just the combination of
Expanded to make the cards split evenly:
At this point we have something like this:
. . .
2. Static gender card
Now let’s create a Gender Card widget that will be used to select gender by a user. Before we get into implementing UI, we need to create a
2.1 Title widget
Let’s start with a title, we can see in the design that the title has the same style for every card, so it will be a good idea to extract it to separate widget:
Let’s add it the
And after that add that card into
This is what we got for now:
2.2 Grey circle
We will display all of our contents in
Stacks since it allows us to draw Widgets on top of each other.
As you can see, there are 2 stacks which at this moment could be avoided, they will stay here because we will need them in later stages. In addition to those stacks, we created simple
GenderCircle widget which surprisingly will draw a circle 🙂 The circle will move down when we add gender icons.
2.3 Gender icons
There are three gender icons, each above the circle but with a different angle. To place them in the right spots we need to move them to the center of the circle, then rotate, then move outside the circle and then rotate again to straighten them up. We will also draw small lines between icons and the circle.
Let’s start with a line:
Now let’s take a look at gender icon widget:
Few things need explaining in this view:
- The default angle is the angle between the middle gender icon and side ones and it is equal to 45 degrees (or pi/4).
- We store angles for each gender outside the widget because we will reuse it when drawing an indicator arrow.
- Since we have access to svg assets, we are using Dan Field’s flutter_svgpackage to load them.
- The Other gender (middle one) is treated a bit differently. Since the middle icon consists of two icons, we make it a bit bigger so that it matches the rest. We also want to move the icon a bit to the right so that it nicely aligns with the grey line.
How we build the widget:
- Draw svg icon.
- Rotate it in the opposite direction to where it will be placed.
- Add a line below the icon (with paddings).
- Rotate icon with a line in the target direction (at this point icon is straight again but the line remained rotated in the desired direction).
- Lift “base” of the widget up half the size of the circle. This will make all icons look like they are centered around the middle of the circle.
It should like this:
2.4 The arrow
Now it’s time to add Arrow widget:
Those are operations performed to get the arrow:
- Draw an svg image.
- Since the image is rotated by default, we want to rotate it back so that arrow is pointing towards the middle.
- We move (translate) an arrow so that it is “pinned” to the center of the screen.
- We rotate arrow to the angle provided in the constructor.
Now let’s use that widget:
. . .
3.1 Handle tapping
Now we can finally allow the user to change the gender on tap. We could add
GestureDetector on icons but they are relatively small, so it would be hard to click them. I figured out that the easiest solution is to split the gender card into 3 equal columns, each assigned to one gender. This way it will be easy for the user to change the gender. Let’s start with GestureDetector:
Again, there is nothing complicated here, we use
Positioned.fill to make
TapHandler fill the whole stack, and then we just handle the taps. We had to make one change regarding the width of the widgets. I removed
build method and replaced it with a
_drawMainStackmethod so that the stack would expand itself to full width.
The last part is to animate the arrow movement. We will do it by creating an
GenderCard and passing it to the
GenderArrowwidget. Let’s get to it:
Ok, so what we’ve done here:
- We added
- We created
AnimationControllerwhich can have values from max left angle to max right angle
- We pass that controller to the
- We changed parent Widget of
AnimatedWidget. This way it will rebuild itself on every controller update.
- We change tap handling not only to set state but also to animate the arrow with the fixed duration.
And that’s it!
This is how the final result for this part looks like:
🔥 BMI Calculator 🔥
Be sure to check out the other posts in the series!
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!
Have you ever woken up and thought “How awesome would it be to create Thanos snap effect in Flutter”? Probably not. Well… following Google’s Easter Egg I did. 🙂 And I’ve found out that no one did it in Flutter yet so why not give it a try! In this post, I will explain how I created a Thanos snap effect coming from Avengers: Infinity War movie. Enjoy 🙂
As developers, we have always get into situations where the designers require some fancy transitions which look awesome on the design but are extremely difficult to implement. Being Flutter developer makes it different, makes it fun because we have tools to do it! To prove it in this post, I will continue implementing awesome Buy Tickets design by Dldp and add bottom sheet transitions!
Parallax effects are awesome. Having elements move in the different speed during scrolling can easily provide the unique feeling for the application and they can make the user think that your app is well-polished. In this post, I will try to achieve parallax effect using PageView, Transforms, Alignments and some basic math.