I've integrated themes into my app using this This idea
I have an EnvironmentKey
that is used throughout the app:
private struct ColorThemeKey: EnvironmentKey {
static let defaultValue = Color.Theme.system
// static let defaultValue = Color.Theme.night
}
extension EnvironmentValues {
var preferredColorTheme: Color.Theme {
get {
return self[ColorThemeKey.self]
}
set {
self[ColorThemeKey.self] = newValue
}
}
}
It works great by changing the default value and relaunching the app.
I'm new to EnvironmentKey
's and i'm struggling to figure out how to allow the user to change this. I have a list of available themes, but how to I overwrite the current them and set a new one that will take over immediately?
When I try to set a new theme like this, it tells me it's get only:
let newTheme = theme.theme
Environment(\.preferredColorTheme).wrappedValue = newTheme