Skip to content

Commit 27fee48

Browse files
authored
CupertinoSwitch: Add an interactive example (#103043)
Update
1 parent 3ed0bbe commit 27fee48

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 CupertinoSwitch
6+
7+
import 'package:flutter/cupertino.dart';
8+
9+
void main() => runApp(const CupertinoSwitchApp());
10+
11+
class CupertinoSwitchApp extends StatelessWidget {
12+
const CupertinoSwitchApp({Key? key}) : super(key: key);
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return const CupertinoApp(
17+
theme: CupertinoThemeData(brightness: Brightness.light),
18+
home: CupertinoSwitchExample(),
19+
);
20+
}
21+
}
22+
23+
class CupertinoSwitchExample extends StatefulWidget {
24+
const CupertinoSwitchExample({Key? key}) : super(key: key);
25+
26+
@override
27+
State<CupertinoSwitchExample> createState() => _CupertinoSwitchExampleState();
28+
}
29+
30+
class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
31+
bool wifi = true;
32+
33+
@override
34+
Widget build(BuildContext context) {
35+
return CupertinoPageScaffold(
36+
navigationBar: const CupertinoNavigationBar(
37+
middle: Text('CupertinoSwitch Sample'),
38+
),
39+
child: Center(
40+
// CupertinoFormRow's main axis is set to max by default.
41+
// Set the intrinsic height widget to center the CupertinoFormRow.
42+
child: IntrinsicHeight(
43+
child: Container(
44+
color: CupertinoTheme.of(context).barBackgroundColor,
45+
child: CupertinoFormRow(
46+
prefix: Row(
47+
children: <Widget>[
48+
Icon(
49+
// Wifi icon is updated based on switch value.
50+
wifi ? CupertinoIcons.wifi : CupertinoIcons.wifi_slash,
51+
color: wifi ? CupertinoColors.systemBlue : CupertinoColors.systemRed,
52+
),
53+
const SizedBox(width: 10),
54+
const Text('Wi-Fi')
55+
],
56+
),
57+
child: CupertinoSwitch(
58+
// This bool value toggles the switch.
59+
value: wifi,
60+
thumbColor: CupertinoColors.systemBlue,
61+
trackColor: CupertinoColors.systemRed.withOpacity(0.14),
62+
activeColor: CupertinoColors.systemRed.withOpacity(0.64),
63+
onChanged: (bool? value) {
64+
// This is called when the user toggles the switch.
65+
setState(() {
66+
wifi = value!;
67+
});
68+
},
69+
),
70+
),
71+
),
72+
),
73+
),
74+
);
75+
}
76+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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/cupertino.dart';
6+
import 'package:flutter_api_samples/cupertino/switch/cupertino_switch.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Toggling cupertino switch updates icon', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.CupertinoSwitchApp(),
13+
);
14+
15+
final Finder switchFinder = find.byType(CupertinoSwitch);
16+
CupertinoSwitch cupertinoSwitch = tester.widget<CupertinoSwitch>(switchFinder);
17+
final Finder wifiOnIcon = find.byIcon(CupertinoIcons.wifi);
18+
final Finder wifiOffIcon = find.byIcon(CupertinoIcons.wifi_slash);
19+
expect(cupertinoSwitch.value, true);
20+
// When the switch is on, wifi icon should be visible.
21+
expect(wifiOnIcon, findsOneWidget);
22+
expect(wifiOffIcon, findsNothing);
23+
24+
await tester.tap(switchFinder);
25+
await tester.pumpAndSettle();
26+
cupertinoSwitch = tester.widget<CupertinoSwitch>(switchFinder);
27+
expect(cupertinoSwitch.value, false);
28+
// When the switch is off, wifi slash icon should be visible.
29+
expect(wifiOnIcon, findsNothing);
30+
expect(wifiOffIcon, findsOneWidget);
31+
});
32+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ import 'thumb_painter.dart';
2626
/// that use a switch will listen for the [onChanged] callback and rebuild the
2727
/// switch with a new [value] to update the visual appearance of the switch.
2828
///
29+
/// {@tool dartpad}
30+
/// This example shows a toggleable [CupertinoSwitch]. When the thumb slides to
31+
/// the other side of the track, the switch is toggled between on/off.
32+
///
33+
/// ** See code in examples/api/lib/cupertino/switch/cupertino_switch.0.dart **
34+
/// {@end-tool}
35+
///
2936
/// {@tool snippet}
3037
///
3138
/// This sample shows how to use a [CupertinoSwitch] in a [ListTile]. The

0 commit comments

Comments
 (0)