mirror of
https://github.com/flutter/flutter
synced 2024-08-28 04:21:14 +00:00
Update CupertinoSwitch
example (#112063)
This commit is contained in:
parent
cf01ecd19e
commit
844a8ad7e8
|
@ -28,7 +28,7 @@ class CupertinoSwitchExample extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
|
||||
bool wifi = true;
|
||||
bool switchValue = true;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -37,38 +37,16 @@ class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
|
|||
middle: Text('CupertinoSwitch Sample'),
|
||||
),
|
||||
child: Center(
|
||||
// CupertinoFormRow's main axis is set to max by default.
|
||||
// Set the intrinsic height widget to center the CupertinoFormRow.
|
||||
child: IntrinsicHeight(
|
||||
child: Container(
|
||||
color: CupertinoTheme.of(context).barBackgroundColor,
|
||||
child: CupertinoFormRow(
|
||||
prefix: Row(
|
||||
children: <Widget>[
|
||||
Icon(
|
||||
// Wifi icon is updated based on switch value.
|
||||
wifi ? CupertinoIcons.wifi : CupertinoIcons.wifi_slash,
|
||||
color: wifi ? CupertinoColors.systemBlue : CupertinoColors.systemRed,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
const Text('Wi-Fi')
|
||||
],
|
||||
),
|
||||
child: CupertinoSwitch(
|
||||
// This bool value toggles the switch.
|
||||
value: wifi,
|
||||
thumbColor: CupertinoColors.systemBlue,
|
||||
trackColor: CupertinoColors.systemRed.withOpacity(0.14),
|
||||
activeColor: CupertinoColors.systemRed.withOpacity(0.64),
|
||||
onChanged: (bool? value) {
|
||||
// This is called when the user toggles the switch.
|
||||
setState(() {
|
||||
wifi = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
child: CupertinoSwitch(
|
||||
// This bool value toggles the switch.
|
||||
value: switchValue,
|
||||
activeColor: CupertinoColors.activeBlue,
|
||||
onChanged: (bool? value) {
|
||||
// This is called when the user toggles the switch.
|
||||
setState(() {
|
||||
switchValue = value ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -14,19 +14,11 @@ void main() {
|
|||
|
||||
final Finder switchFinder = find.byType(CupertinoSwitch);
|
||||
CupertinoSwitch cupertinoSwitch = tester.widget<CupertinoSwitch>(switchFinder);
|
||||
final Finder wifiOnIcon = find.byIcon(CupertinoIcons.wifi);
|
||||
final Finder wifiOffIcon = find.byIcon(CupertinoIcons.wifi_slash);
|
||||
expect(cupertinoSwitch.value, true);
|
||||
// When the switch is on, wifi icon should be visible.
|
||||
expect(wifiOnIcon, findsOneWidget);
|
||||
expect(wifiOffIcon, findsNothing);
|
||||
|
||||
await tester.tap(switchFinder);
|
||||
await tester.pumpAndSettle();
|
||||
cupertinoSwitch = tester.widget<CupertinoSwitch>(switchFinder);
|
||||
expect(cupertinoSwitch.value, false);
|
||||
// When the switch is off, wifi slash icon should be visible.
|
||||
expect(wifiOnIcon, findsNothing);
|
||||
expect(wifiOffIcon, findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue