Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
27fee486
Unverified
Commit
27fee486
authored
May 05, 2022
by
Taha Tesser
Committed by
GitHub
May 05, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
`CupertinoSwitch`: Add an interactive example (#103043)
Update
parent
3ed0bbed
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
0 deletions
+115
-0
cupertino_switch.0.dart
examples/api/lib/cupertino/switch/cupertino_switch.0.dart
+76
-0
cupertino_switch.0_test.dart
...es/api/test/cupertino/switch/cupertino_switch.0_test.dart
+32
-0
switch.dart
packages/flutter/lib/src/cupertino/switch.dart
+7
-0
No files found.
examples/api/lib/cupertino/switch/cupertino_switch.0.dart
0 → 100644
View file @
27fee486
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flutter code sample for CupertinoSwitch
import
'package:flutter/cupertino.dart'
;
void
main
(
)
=>
runApp
(
const
CupertinoSwitchApp
());
class
CupertinoSwitchApp
extends
StatelessWidget
{
const
CupertinoSwitchApp
({
Key
?
key
})
:
super
(
key:
key
);
@override
Widget
build
(
BuildContext
context
)
{
return
const
CupertinoApp
(
theme:
CupertinoThemeData
(
brightness:
Brightness
.
light
),
home:
CupertinoSwitchExample
(),
);
}
}
class
CupertinoSwitchExample
extends
StatefulWidget
{
const
CupertinoSwitchExample
({
Key
?
key
})
:
super
(
key:
key
);
@override
State
<
CupertinoSwitchExample
>
createState
()
=>
_CupertinoSwitchExampleState
();
}
class
_CupertinoSwitchExampleState
extends
State
<
CupertinoSwitchExample
>
{
bool
wifi
=
true
;
@override
Widget
build
(
BuildContext
context
)
{
return
CupertinoPageScaffold
(
navigationBar:
const
CupertinoNavigationBar
(
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
!;
});
},
),
),
),
),
),
);
}
}
examples/api/test/cupertino/switch/cupertino_switch.0_test.dart
0 → 100644
View file @
27fee486
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter/cupertino.dart'
;
import
'package:flutter_api_samples/cupertino/switch/cupertino_switch.0.dart'
as
example
;
import
'package:flutter_test/flutter_test.dart'
;
void
main
(
)
{
testWidgets
(
'Toggling cupertino switch updates icon'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
const
example
.
CupertinoSwitchApp
(),
);
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
);
});
}
packages/flutter/lib/src/cupertino/switch.dart
View file @
27fee486
...
@@ -26,6 +26,13 @@ import 'thumb_painter.dart';
...
@@ -26,6 +26,13 @@ import 'thumb_painter.dart';
/// that use a switch will listen for the [onChanged] callback and rebuild the
/// that use a switch will listen for the [onChanged] callback and rebuild the
/// switch with a new [value] to update the visual appearance of the switch.
/// switch with a new [value] to update the visual appearance of the switch.
///
///
/// {@tool dartpad}
/// This example shows a toggleable [CupertinoSwitch]. When the thumb slides to
/// the other side of the track, the switch is toggled between on/off.
///
/// ** See code in examples/api/lib/cupertino/switch/cupertino_switch.0.dart **
/// {@end-tool}
///
/// {@tool snippet}
/// {@tool snippet}
///
///
/// This sample shows how to use a [CupertinoSwitch] in a [ListTile]. The
/// This sample shows how to use a [CupertinoSwitch] in a [ListTile]. The
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment