Some time ago I started to play with flutter and dart. As for me - it looks very good: language available functionality within, platforms and support for various features - looks very promising.
I trying to make some functionality, and below I just want to describe how to make a battery indicator using Flutter and Dart.
Problem
We have a device with a battery to which our app can connect and read the battery state. This information needs to be shown to the user. To do so, we may want to create some ui component, that can visualize info and any change related to it.
Solution
To achieve our target we need to split the task into separate parts:
describe battery status and state for it BatteryStatusType and BatteryStatus
divide the battery indicator into separate elements and draw each separately, combining them into one element on the last step
The elements can be as follows:
The idea for this components is grabbed from here and here
Define a state
Of course, we can start from the easiest part - BatteryStatus. The idea is to describe states and all elements needed to draw it - colors, value, etc.
I decided to define possible types of status in a structure named BatteryStatusType as follows:
I guess no description is needed here.
To define actual value and rendering aspects, here is another structure that describes the actual state of the battery - BatteryStatus:
Making indicator
Tools
BatteryIndicator will be components that use this structure to determine the current state and to show it on screen.
Before we go to the actual design, it’s good to learn about a few elements available in a flutter:
Decoration - this class can be used to change and decorate some rect - container or other structure that defines it. We can use it for changing the background, border, and other aspects. Withing our component we need to have round corners and different background colors, so this is exactly what we need.
AnimatedContainer - this version of the container (or rect if u want), that automatically animate changes in it - for example background color or element size - we can use it to apply animation to our track inside the battery indicator. One more thing - thus AnimatedContainer is a rect, we can apply any Decoration to it.
LayoutBuilder - this object can provide us parent widget’s constraints, so we can use it to determine the correct geometry for our indicator inside the battery bar
AnimatedSwitcher - one more handy stuff - this container allows to automatically animate change of widget. Thus our indicators are only visible when the charge is low (BatteryStatusType.low), Within this container, we receive appearance animation for free.
AnimationController - this one will help us to animate our icon for low charge (blinking animation will grab more attention from user to low charge state)
That’s our tool for this component.
Drawing
As I mentioned above - all that is needed is just divide our component into small parts and draw each part separately.
Thus we have a few components - we must arrange them when they are drawn. To do so, we can use Row and Stack.
Note: in such a way we could easily change the orientation of the battery indicator - just add some properties that define supported bat orientation and combine in different order sub-components for the indicator. Like Flutter suggests - build and use everything as a constructor.
Battery tracks are combined from 2 more parts - some background and track:
Here is good to mention, that all containers and other components can be modified by using Decoration:
The most interesting part here is a batteryBar - using AnimatedContainer we can animate its width aka charge level:
The last part is our knob - it’s just a rect (Container), that is placed in a Row near the batteryTrack:
The full code is here
Using the code above, we can receive the next element:
Notice, that the icon is not animated, but we want to make it blinking. To achieve this, let’s build one more widget, that will make this happen. We can use FadeTransition to fade in and fade-out the icon:
The full code for element
By replacing the Icon element with this blinking widget:
we can receive this:
Conclusion
Always inspect available functionality in API - u will wonder, how many times the things that u want to create are already prepared for u.