Skip to content

Commit 844a8ad

Browse files
authored
Update CupertinoSwitch example (#112063)
1 parent cf01ecd commit 844a8ad

File tree

2 files changed

+11
-41
lines changed

2 files changed

+11
-41
lines changed

examples/api/lib/cupertino/switch/cupertino_switch.0.dart

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CupertinoSwitchExample extends StatefulWidget {
2828
}
2929

3030
class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
31-
bool wifi = true;
31+
bool switchValue = true;
3232

3333
@override
3434
Widget build(BuildContext context) {
@@ -37,38 +37,16 @@ class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
3737
middle: Text('CupertinoSwitch Sample'),
3838
),
3939
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-
),
40+
child: CupertinoSwitch(
41+
// This bool value toggles the switch.
42+
value: switchValue,
43+
activeColor: CupertinoColors.activeBlue,
44+
onChanged: (bool? value) {
45+
// This is called when the user toggles the switch.
46+
setState(() {
47+
switchValue = value ?? false;
48+
});
49+
},
7250
),
7351
),
7452
);

examples/api/test/cupertino/switch/cupertino_switch.0_test.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@ void main() {
1414

1515
final Finder switchFinder = find.byType(CupertinoSwitch);
1616
CupertinoSwitch cupertinoSwitch = tester.widget<CupertinoSwitch>(switchFinder);
17-
final Finder wifiOnIcon = find.byIcon(CupertinoIcons.wifi);
18-
final Finder wifiOffIcon = find.byIcon(CupertinoIcons.wifi_slash);
1917
expect(cupertinoSwitch.value, true);
20-
// When the switch is on, wifi icon should be visible.
21-
expect(wifiOnIcon, findsOneWidget);
22-
expect(wifiOffIcon, findsNothing);
2318

2419
await tester.tap(switchFinder);
2520
await tester.pumpAndSettle();
2621
cupertinoSwitch = tester.widget<CupertinoSwitch>(switchFinder);
2722
expect(cupertinoSwitch.value, false);
28-
// When the switch is off, wifi slash icon should be visible.
29-
expect(wifiOnIcon, findsNothing);
30-
expect(wifiOffIcon, findsOneWidget);
3123
});
3224
}

0 commit comments

Comments
 (0)