import SwiftUI
struct AppearanceView: View {
@Environment(\.colorScheme) var colorScheme
@State private var selectedAppearance: Appearance = .deviceSettings
enum Appearance: String, CaseIterable, Identifiable {
case light = "Light"
case dark = "Dark"
case deviceSettings = "Device Settings"
var id: String { self.rawValue }
}
var body: some View {
NavigationView {
VStack(alignment: .leading, spacing: 16) {
Text("Appearance")
.font(.title)
.bold()
Picker("Appearance", selection: $selectedAppearance) {
ForEach(Appearance.allCases) { appearance in
Text(appearance.rawValue)
}
}
.pickerStyle(MenuPickerStyle())
Spacer()
}
.padding()
.navigationBarTitle("Appearance")
.navigationBarItems(leading: BackButton())
}
.preferredColorScheme(getPreferredColorScheme())
}
private func getPreferredColorScheme() -> ColorScheme? {
switch selectedAppearance {
case .light:
return .light
case .dark:
return .dark
case .deviceSettings:
return nil
}
}
}
struct BackButton: View {
var body: some View {
NavigationLink(destination: MoreView()) {
Image(systemName: "chevron.left")
.font(.title2)
}
}
}
struct MoreView: View {
var body: some View {
Text("More")
.font(.title)
.bold()
}
}
struct AppearanceView_Previews: PreviewProvider {
static var previews: some View {
Group {
AppearanceView()
.environment(\.colorScheme, .light)
AppearanceView()
.environment(\.colorScheme, .dark)
}
}
}