Skip to content

Commit 5fba084

Browse files
committed
Restore the List and Grid gallery screens
1 parent 744e443 commit 5fba084

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

Demo/App.swiftpm/Sources/ContentView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct ContentView: View {
1616
NavigationLink("Text", destination: TextScreen())
1717
NavigationLink("Buttons", destination: ButtonScreen())
1818
NavigationLink("Stacks", destination: StacksScreen())
19+
NavigationLink("List", destination: ListScreen())
20+
NavigationLink("Grid", destination: GridScreen())
1921
NavigationLink("State", destination: StateScreen())
2022
NavigationLink("Controls", destination: ControlsScreen())
2123
NavigationLink("Modifiers", destination: ModifierScreen())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
/// Lazy grids.
8+
struct GridScreen: View {
9+
10+
var body: some View {
11+
VStack(spacing: 16) {
12+
Text("Three fixed columns")
13+
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
14+
ForEach(1...9, id: \.self) { number in
15+
Text("\(number)")
16+
.padding()
17+
.background(Color.blue)
18+
}
19+
}
20+
Divider()
21+
Text("Adaptive columns")
22+
LazyVGrid(columns: [GridItem(.adaptive(minimum: 80))]) {
23+
ForEach(1...8, id: \.self) { number in
24+
Text("Item \(number)")
25+
.padding()
26+
.background(Color.orange)
27+
}
28+
}
29+
Divider()
30+
Text("Two rows, horizontal")
31+
LazyHGrid(rows: [GridItem(.flexible()), GridItem(.flexible())]) {
32+
ForEach(1...8, id: \.self) { number in
33+
Text("Cell \(number)")
34+
.padding()
35+
.background(Color.green)
36+
}
37+
}
38+
.frame(height: 160)
39+
}
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
/// A lazy list with pull to refresh. Rows are evaluated on demand, so the
8+
/// large-list variant scrolls without materializing every row up front.
9+
struct ListScreen: View {
10+
11+
@State
12+
private var items = (1...30).map(ListItem.init)
13+
14+
var body: some View {
15+
List(items) { item in
16+
Text(item.title)
17+
}
18+
.refreshable {
19+
await addItem()
20+
}
21+
}
22+
23+
private func addItem() async {
24+
try? await Task.sleep(for: .seconds(1))
25+
items.insert(ListItem(id: items.count + 1), at: 0)
26+
}
27+
}
28+
29+
struct ListItem: Identifiable {
30+
31+
let id: Int
32+
33+
var title: String {
34+
"Row \(id)"
35+
}
36+
}

0 commit comments

Comments
 (0)