| // Copyright 2014 The Flutter Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import 'package:flutter/material.dart'; | |
| /// Flutter code sample for [Switch]. | |
| void main() => runApp(const SwitchApp()); | |
| class SwitchApp extends StatelessWidget { | |
| const SwitchApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: Scaffold( | |
| appBar: AppBar(title: const Text('Switch Sample')), | |
| body: const Center(child: SwitchExample()), | |
| ), | |
| ); | |
| } | |
| } | |
| class SwitchExample extends StatefulWidget { | |
| const SwitchExample({super.key}); | |
| @override | |
| State<SwitchExample> createState() => _SwitchExampleState(); | |
| } | |
| class _SwitchExampleState extends State<SwitchExample> { | |
| bool light0 = true; | |
| bool light1 = true; | |
| static const WidgetStateProperty<Icon> thumbIcon = | |
| WidgetStateProperty<Icon>.fromMap(<WidgetStatesConstraint, Icon>{ | |
| WidgetState.selected: Icon(Icons.check), | |
| WidgetState.any: Icon(Icons.close), | |
| }); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column( | |
| mainAxisAlignment: .center, | |
| children: <Widget>[ | |
| Switch( | |
| value: light0, | |
| onChanged: (bool value) { | |
| setState(() { | |
| light0 = value; | |
| }); | |
| }, | |
| ), | |
| Switch( | |
| thumbIcon: thumbIcon, | |
| value: light1, | |
| onChanged: (bool value) { | |
| setState(() { | |
| light1 = value; | |
| }); | |
| }, | |
| ), | |
| ], | |
| ); | |
| } | |
| } |