This post will be a bit different from the previous ones:
- 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 I those are my main concerns for that project. This allows us to use 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 use to use the scaling both in landcscape and portrait. Thanks to Simon Lightfoot
1. Input screen layout
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. 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 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_svg package 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.
After adding icons to the Stack…
…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 constructor.
Now let’s use that widget:
3.1 Handle tapping
Now it’s time to 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 Gesture Detector:
As you can see, there is nothing complicated here. Now let’s use that widget:
Again, there is nothing complicated here, we use
Positioned.fill to make
TapHandler fill 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
_drawMainStack method 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
GenderArrow widget. 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:
I hope you liked this article, you can find full code for this part here.
If you find something hard to understand or you have better ideas, feel free to leave a comment!