ToggleSwitch is a simple customizable Toggle button with state management, will be right widget for making choice. Combine with con and Text , or make it colorful with colors from your favorite pallet.
Dependency and import
dependencies:
flutter:
sdk: flutter
toggle_switch: "^0.1.8"
import 'package:toggle_switch/toggle_switch.dart';

A sample ToggleSwitch widget with Icon
ToggleSwitch(
minWidth: 90.0,
initialLabelIndex: 1,
cornerRadius: 20.0,
activeFgColor: Colors.white,
inactiveBgColor: Colors.grey,
inactiveFgColor: Colors.white,
labels: ['Male', 'Female'],
icons: [FontAwesomeIcons.mars, FontAwesomeIcons.venus],
activeBgColors: [Colors.blue, Colors.pink],
onToggle: (index) {
print('switched to: $index');
},
),
You can use initialLabelIndex
as a setter for the widget like editing page. Create variable for initial state such as int mychoice=0; and set it as intitialLabelIndex`and use it as setter say mychoice=1.
onToggle: (index) {
switch (index) {
case 0:
//Todo
break;
case 1:
//Todo
break;
case 2:
//Todo
break;
}
The OnToggle button can be used with if /witch statement so, that you can handle some action while user select the button.