Created
December 17, 2022 07:08
-
-
Save StewartLynch/fd0608bd3992ee2d04e5552c0450375f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
enum Style: String, CaseIterable { | |
case grouped, inset, insetGrouped, plain, sidebar | |
} | |
struct ContentView: View { | |
@State private var selectedStyle: Style = .plain | |
var body: some View { | |
VStack { | |
Picker("List Style", selection: $selectedStyle) { | |
ForEach(Style.allCases, id: \.self) { style in | |
Text(style.rawValue) | |
} | |
} | |
List { | |
Section("Breakfast") { | |
Text("pancakes") | |
Text("bacon") | |
Text("orange juice") | |
} | |
Section("Lunch") { | |
Text("sandwich") | |
Text("chips") | |
Text("lemonade") | |
} | |
Section("Dinner") { | |
Text("spaghetti") | |
Text("bread") | |
Text("water") | |
} | |
} | |
.listStyle(for: selectedStyle) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
extension List { | |
@ViewBuilder | |
func listStyle(for customListStyle: Style) -> some View { | |
switch customListStyle { | |
case .grouped: | |
listStyle(.grouped) | |
case .inset: | |
listStyle(.plain) | |
case .insetGrouped: | |
listStyle(.insetGrouped) | |
case .plain: | |
listStyle(.plain) | |
case .sidebar: | |
listStyle(.sidebar) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment