mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
74 lines
1.7 KiB
Dart
74 lines
1.7 KiB
Dart
// 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(
|
|
theme: ThemeData(useMaterial3: true),
|
|
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;
|
|
|
|
final MaterialStateProperty<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>(
|
|
(Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.selected)) {
|
|
return const Icon(Icons.check);
|
|
}
|
|
return const Icon(Icons.close);
|
|
},
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Switch(
|
|
value: light0,
|
|
onChanged: (bool value) {
|
|
setState(() {
|
|
light0 = value;
|
|
});
|
|
},
|
|
),
|
|
Switch(
|
|
thumbIcon: thumbIcon,
|
|
value: light1,
|
|
onChanged: (bool value) {
|
|
setState(() {
|
|
light1 = value;
|
|
});
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|