In this article, we are going to create a minimal custom Switch Widget in Flutter. Flutter provides a built-in Switch Widget which looks simple. We are going to create our own Flat Style switch widget.
We will start writing code for the base and the knob separately.
The base is nothing but the background of the switch which is in a rounded rectangle shape.
AnimatedContainervis used instead of the container because we are going to add animations when the switch is turned ON and OFF.
widget.value holds either true or false which represents whether a switch is ON or OFF respectively
The knob is the circular button inside the base that we use to toggle the state of the switch.
- AnimatedAlign is used to animate the movement of the switch when the user clicks it.
The final output looks as follows:
Take a look at the below Gist to view the full code with functionality:
Well, that’s everything! It’s a short and sweet tutorial but I hope you found it helpful.