@@ -28,7 +28,7 @@ class CupertinoSwitchExample extends StatefulWidget {
28
28
}
29
29
30
30
class _CupertinoSwitchExampleState extends State <CupertinoSwitchExample > {
31
- bool wifi = true ;
31
+ bool switchValue = true ;
32
32
33
33
@override
34
34
Widget build (BuildContext context) {
@@ -37,38 +37,16 @@ class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
37
37
middle: Text ('CupertinoSwitch Sample' ),
38
38
),
39
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
- ),
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
+ },
72
50
),
73
51
),
74
52
);
0 commit comments