I'm using a toggle and @AppStorage to change the preferredColorScheme in my app. Dark mode and light mode works fine on Views other than sheets and full screen modals.
struct MyApp: App {
@AppStorage("darkMode") var darkMode = false
init() {
FirebaseApp.configure()
}
var body: some Scene {
WindowGroup {
ContentView()
.preferredColorScheme(darkMode ? .dark : .light)
}
}
}
App Settings View:
struct AppSettingsView: View {
@ObservedObject var appSettingsVM: AppSettingsViewModel
var body: some View {
ScrollView {
Toggle("Dark Mode", isOn: $appSettingsVM.darkMode.animation())
.toggleStyle(SwitchToggleStyle(tint: .orange))
.padding()
}
}
}
App Settings View Model:
class AppSettingsViewModel: ObservableObject {
@AppStorage("darkMode") var darkMode: Bool = false
}
There are multiple sheets and full screen modals under the Content View hierarchy and none of them respond to the changes in Color Scheme.
I modified the solution from a YouTube video and this works.
Video link: https://www.youtube.com/watch?v=j7a4jvHz4MM