Skip to content

Commit 97452d1

Browse files
authored
Switch: Add an interactive example (#103045)
* `Switch`: Add an interactive examples * Update docs * Update doc
1 parent 680a819 commit 97452d1

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
5+
// Flutter code sample for Switch
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const SwitchApp());
10+
11+
class 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(
20+
child: SwitchExample(),
21+
),
22+
),
23+
);
24+
}
25+
}
26+
27+
class SwitchExample extends StatefulWidget {
28+
const SwitchExample({super.key});
29+
30+
@override
31+
State<SwitchExample> createState() => _SwitchExampleState();
32+
}
33+
34+
class _SwitchExampleState extends State<SwitchExample> {
35+
bool light = true;
36+
37+
@override
38+
Widget build(BuildContext context) {
39+
return Switch(
40+
// This bool value toggles the switch.
41+
value: light,
42+
activeColor: Colors.red,
43+
onChanged: (bool value) {
44+
// This is called when the user toggles the switch.
45+
setState(() {
46+
light = value;
47+
});
48+
},
49+
);
50+
}
51+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
5+
// Flutter code sample for Switch
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const SwitchApp());
10+
11+
class 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(
20+
child: SwitchExample(),
21+
),
22+
),
23+
);
24+
}
25+
}
26+
27+
class SwitchExample extends StatefulWidget {
28+
const SwitchExample({super.key});
29+
30+
@override
31+
State<SwitchExample> createState() => _SwitchExampleState();
32+
}
33+
34+
class _SwitchExampleState extends State<SwitchExample> {
35+
bool light = true;
36+
37+
@override
38+
Widget build(BuildContext context) {
39+
final MaterialStateProperty<Color?> trackColor = MaterialStateProperty.resolveWith<Color?>(
40+
(Set<MaterialState> states) {
41+
// Track color when the switch is selected.
42+
if (states.contains(MaterialState.selected)) {
43+
return Colors.amber;
44+
}
45+
// Otherwise return null to set default track color
46+
// for remaining states such as when the switch is
47+
// hovered, focused, or disabled.
48+
return null;
49+
},
50+
);
51+
final MaterialStateProperty<Color?> overlayColor = MaterialStateProperty.resolveWith<Color?>(
52+
(Set<MaterialState> states) {
53+
// Material color when switch is selected.
54+
if (states.contains(MaterialState.selected)) {
55+
return Colors.amber.withOpacity(0.54);
56+
}
57+
// Material color when switch is disabled.
58+
if (states.contains(MaterialState.disabled)) {
59+
return Colors.grey.shade400;
60+
}
61+
// Otherwise return null to set default material color
62+
// for remaining states such as when the switch is
63+
// hovered, or focused.
64+
return null;
65+
},
66+
);
67+
68+
return Switch(
69+
// This bool value toggles the switch.
70+
value: light,
71+
overlayColor: overlayColor,
72+
trackColor: trackColor,
73+
thumbColor: MaterialStateProperty.all<Color>(Colors.black),
74+
onChanged: (bool value) {
75+
// This is called when the user toggles the switch.
76+
setState(() {
77+
light = value;
78+
});
79+
},
80+
);
81+
}
82+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/switch/switch.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Can toggle switch', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.SwitchApp(),
13+
);
14+
15+
final Finder switchFinder = find.byType(Switch);
16+
Switch materialSwitch = tester.widget<Switch>(switchFinder);
17+
expect(materialSwitch.value, true);
18+
19+
await tester.tap(switchFinder);
20+
await tester.pumpAndSettle();
21+
materialSwitch = tester.widget<Switch>(switchFinder);
22+
expect(materialSwitch.value, false);
23+
});
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/switch/switch.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Can toggle switch', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.SwitchApp(),
13+
);
14+
15+
final Finder switchFinder = find.byType(Switch);
16+
Switch materialSwitch = tester.widget<Switch>(switchFinder);
17+
expect(materialSwitch.value, true);
18+
19+
await tester.tap(switchFinder);
20+
await tester.pumpAndSettle();
21+
materialSwitch = tester.widget<Switch>(switchFinder);
22+
expect(materialSwitch.value, false);
23+
});
24+
}

packages/flutter/lib/src/material/switch.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,29 @@ enum _SwitchType { material, adaptive }
4444
///
4545
/// Requires one of its ancestors to be a [Material] widget.
4646
///
47+
/// {@tool dartpad}
48+
/// This example shows a toggleable [Switch]. When the thumb slides to the other
49+
/// side of the track, the switch is toggled between on/off.
50+
///
51+
/// ** See code in examples/api/lib/material/switch/switch.0.dart **
52+
/// {@end-tool}
53+
///
54+
/// {@tool dartpad}
55+
/// This example shows how to customize [Switch] using [MaterialStateProperty]
56+
/// switch properties.
57+
///
58+
/// ** See code in examples/api/lib/material/switch/switch.1.dart **
59+
/// {@end-tool}
60+
///
4761
/// See also:
4862
///
4963
/// * [SwitchListTile], which combines this widget with a [ListTile] so that
5064
/// you can give the switch a label.
5165
/// * [Checkbox], another widget with similar semantics.
5266
/// * [Radio], for selecting among a set of explicit values.
5367
/// * [Slider], for selecting a value in a range.
68+
/// * [MaterialStateProperty], an interface for objects that "resolve" to
69+
/// different values depending on a widget's material state.
5470
/// * <https://material.io/design/components/selection-controls.html#switches>
5571
class Switch extends StatelessWidget {
5672
/// Creates a Material Design switch.

0 commit comments

Comments
 (0)