Flutter Switch Example
Hi developer in these Flutter tutorial we shared how to used, Flutter Switch is used to toggle a setting between Switch on/ Switch off which is true/false respectively.
When the switch is on, the value returned by the Switch onChanged property is true, and the switch is off, onChanged property returns false.
Example of Flutter Switch Widget.
In the this example Flutter application, we used a Switch button. Whenever the Switch button is toggled, onChanged is called with new state of Switch as value.
We have defined a boolean variable for isSwitched to store the state of Switch. Create an Flutter application and copy this code to past on your main.dart with the following code.
Main.Dart File Source code.
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( home: MyApp(), )); } class MyApp extends StatefulWidget { @override _State createState() => _State(); } class _State extends State<MyApp> { bool isSwitched = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flutter - codeplayon.com'), ), body: Center( child: Switch( value: isSwitched, onChanged: (value) { setState(() { isSwitched = value; print(isSwitched); }); }, activeTrackColor: Colors.lightBlueAccent, activeColor: Colors.blue, ), ) ); } }
Now run your Application Happy Coding