Display Switch in Flutter

Display Switch in Flutter

A switch is a two-state input control that allows to toggle a setting between ON/OFF states.

A Switch class is used to create a switch. The onChanged callback is called when the user changed the state of the switch. The value property holds current state (true or false).

import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _value = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Switch')),
        body: Center(
            child: Switch(
          value: _value,
          onChanged: (value) {
            setState(() => _value = value);
          },
        )));
  }
}

Leave a Comment

Cancel reply

Your email address will not be published.