A switch is a two-state user interface element used to toggle between on (checked) or off (unchecked) states. Typically, this is a button with a thumb slider where the user can drag back and forth to select an option as on or off. Its function is similar to that of a household electrical switch.
In Flutter, a switch is a widget used to choose between two options, either on or off. It does not maintain state itself. To maintain the states, it will call the onChanged property. If the value return by this property is TRUE, then the switch is ON and FALSE if OFF. When this property is null, the Switch widget is disabled. In this article, we are going to understand how to use Switch widget in Flutter application.
In this tutorials we add some new dependency injection flutter like lite_rolling_switch. see how to impliment and use.
Step 1: Open pubspec.yaml file and add some dependency and click Pub get option in the top right.
lite_rolling_switch: ^1.0.0
Step 2: Add this code where you want and also customize what you want
LiteRollingSwitch(
value: widget.status,
width: 150,
textOn: 'active',
textOff: 'inactive',
colorOn: Colors.deepOrange,
colorOff: Colors.blueGrey,
iconOn: Icons.lightbulb_outline,
iconOff: Icons.power_settings_new,
animationDuration: const Duration(milliseconds: 300),
onChanged: (bool state) {
print('turned ${(state) ? 'on' : 'off'}');
setState(() {
if(state == true){
//code when it's true
}
else if(state == false){
//code when it's false
}
else{
print('we cant process this time!!!');
}
});
},
onDoubleTap: () {
//code when double tap
},
onSwipe: () {
//code when swipe
},
onTap: () {
//code when tap
},
),