1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7/// Flutter code sample for [Switch].
8
9void main() => runApp(const SwitchApp());
10
11class SwitchApp extends StatelessWidget {
12 const SwitchApp({super.key});
13
14 @override
15 Widget build(BuildContext context) {
16 return MaterialApp(
17 home: Scaffold(
18 appBar: AppBar(title: const Text('Switch Sample')),
19 body: const Center(child: SwitchExample()),
20 ),
21 );
22 }
23}
24
25class SwitchExample extends StatefulWidget {
26 const SwitchExample({super.key});
27
28 @override
29 State<SwitchExample> createState() => _SwitchExampleState();
30}
31
32class _SwitchExampleState extends State<SwitchExample> {
33 bool light0 = true;
34 bool light1 = true;
35
36 static const WidgetStateProperty<Icon> thumbIcon = WidgetStateProperty<Icon>.fromMap(
37 <WidgetStatesConstraint, Icon>{
38 WidgetState.selected: Icon(Icons.check),
39 WidgetState.any: Icon(Icons.close),
40 },
41 );
42
43 @override
44 Widget build(BuildContext context) {
45 return Column(
46 mainAxisAlignment: MainAxisAlignment.center,
47 children: <Widget>[
48 Switch(
49 value: light0,
50 onChanged: (bool value) {
51 setState(() {
52 light0 = value;
53 });
54 },
55 ),
56 Switch(
57 thumbIcon: thumbIcon,
58 value: light1,
59 onChanged: (bool value) {
60 setState(() {
61 light1 = value;
62 });
63 },
64 ),
65 ],
66 );
67 }
68}
69