1. import 'package:flutter/material.dart';
    2. void main() {
    3. runApp(MyApp());
    4. }
    5. class MyApp extends StatelessWidget {
    6. @override
    7. Widget build(BuildContext context) {
    8. return MaterialApp(
    9. debugShowCheckedModeBanner: false,
    10. home: Home(),
    11. );
    12. }
    13. }
    14. class Home extends StatefulWidget {
    15. @override
    16. _HomeState createState() => _HomeState();
    17. }
    18. class _HomeState extends State<Home> {
    19. bool isSwitched = false;
    20. @override
    21. Widget build(BuildContext context) {
    22. return Scaffold(
    23. appBar: AppBar(
    24. backgroundColor: Colors.green,
    25. title: Text("Flutter Switch Example"),
    26. ),
    27. body: Center(
    28. child: Switch(
    29. value: isSwitched,
    30. onChanged: (value){
    31. setState(() {
    32. isSwitched=value;
    33. print(isSwitched);
    34. });
    35. },
    36. activeTrackColor: Colors.lightGreenAccent,
    37. activeColor: Colors.green,
    38. ),
    39. ),
    40. );
    41. }
    42. }