Skip to content

Commit 43bab17

Browse files
committed
Add control playgrounds (Text, Button, Toggle, Slider, TextField, Picker, ProgressView)
1 parent 63b1705 commit 43bab17

1 file changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
struct TextPlayground: View {
8+
@State private var counter = 0
9+
var body: some View {
10+
ScrollView {
11+
VStack(alignment: .leading, spacing: 0) {
12+
Example("Plain") { Text("Hello, world") }
13+
Example("Verbatim") { Text(verbatim: "Raw string, no interpolation") }
14+
Example("Interpolated") { Text("Counter is \(counter)") }
15+
Example("Bump the counter") { Button("Increment") { counter += 1 } }
16+
Example("Multiline") {
17+
Text("A longer passage of text that wraps onto multiple lines when it no longer fits within the width of the screen.")
18+
}
19+
Example("Styled") {
20+
Text("Blue on a rounded chip")
21+
.padding()
22+
.background(Color.blue)
23+
.cornerRadius(12)
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
struct ButtonPlayground: View {
31+
@State private var taps = 0
32+
var body: some View {
33+
ScrollView {
34+
VStack(alignment: .leading, spacing: 0) {
35+
Example("Taps: \(taps)") { Text("Tap any button below") }
36+
Example("Title initializer") { Button("Tap me") { taps += 1 } }
37+
Example("Label closure") {
38+
Button(action: { taps += 1 }) { Text("Custom label") }
39+
}
40+
Example("Styled label") {
41+
Button(action: { taps += 1 }) {
42+
Text("Padded label")
43+
.padding()
44+
.background(Color.green)
45+
.cornerRadius(8)
46+
}
47+
}
48+
Example("Reset") { Button("Reset to zero") { taps = 0 } }
49+
}
50+
}
51+
}
52+
}
53+
54+
struct TogglePlayground: View {
55+
@State private var a = false
56+
@State private var b = true
57+
var body: some View {
58+
ScrollView {
59+
VStack(alignment: .leading, spacing: 0) {
60+
Example("Off by default") {
61+
VStack(alignment: .leading, spacing: 8) {
62+
Toggle("Enable feature", isOn: $a)
63+
Text(a ? "Feature is on" : "Feature is off")
64+
}
65+
}
66+
Example("On by default") {
67+
VStack(alignment: .leading, spacing: 8) {
68+
Toggle("Notifications", isOn: $b)
69+
Text(b ? "Notifications on" : "Notifications off")
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
77+
struct SliderPlayground: View {
78+
@State private var unit = 0.5
79+
@State private var wide = 25.0
80+
var body: some View {
81+
ScrollView {
82+
VStack(alignment: .leading, spacing: 0) {
83+
Example("0...1") {
84+
VStack(alignment: .leading, spacing: 8) {
85+
Slider(value: $unit, in: 0...1)
86+
Text("Value: \(Int(unit * 100))%")
87+
}
88+
}
89+
Example("0...100") {
90+
VStack(alignment: .leading, spacing: 8) {
91+
Slider(value: $wide, in: 0...100)
92+
Text("Value: \(Int(wide))")
93+
}
94+
}
95+
}
96+
}
97+
}
98+
}
99+
100+
struct TextFieldPlayground: View {
101+
@State private var name = ""
102+
@State private var city = ""
103+
var body: some View {
104+
ScrollView {
105+
VStack(alignment: .leading, spacing: 0) {
106+
Example("Name") {
107+
VStack(alignment: .leading, spacing: 8) {
108+
TextField("Your name", text: $name)
109+
Text(name.isEmpty ? "Nothing typed yet" : "Hello, \(name)")
110+
}
111+
}
112+
Example("City") {
113+
VStack(alignment: .leading, spacing: 8) {
114+
TextField("Your city", text: $city)
115+
Text(city.isEmpty ? "" : "You are in \(city)")
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
123+
struct PickerPlayground: View {
124+
@State private var fruit = "Apple"
125+
@State private var size = 1
126+
var body: some View {
127+
ScrollView {
128+
VStack(alignment: .leading, spacing: 0) {
129+
Example("String selection") {
130+
VStack(alignment: .leading, spacing: 8) {
131+
Picker("Fruit", selection: $fruit) {
132+
Text("Apple").tag("Apple")
133+
Text("Banana").tag("Banana")
134+
Text("Cherry").tag("Cherry")
135+
}
136+
Text("Selected: \(fruit)")
137+
}
138+
}
139+
Example("Int selection") {
140+
VStack(alignment: .leading, spacing: 8) {
141+
Picker("Size", selection: $size) {
142+
Text("Small").tag(0)
143+
Text("Medium").tag(1)
144+
Text("Large").tag(2)
145+
}
146+
Text("Size index: \(size)")
147+
}
148+
}
149+
}
150+
}
151+
}
152+
}
153+
154+
struct ProgressViewPlayground: View {
155+
@State private var progress = 0.25
156+
var body: some View {
157+
ScrollView {
158+
VStack(alignment: .leading, spacing: 0) {
159+
Example("Indeterminate") { ProgressView() }
160+
Example("Determinate") {
161+
VStack(alignment: .leading, spacing: 8) {
162+
ProgressView(value: progress)
163+
Text("\(Int(progress * 100))%")
164+
Button("Advance") { progress = progress >= 1 ? 0 : progress + 0.25 }
165+
}
166+
}
167+
}
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)