[FREQ] Problems with using `@Environment(\.colorScheme) var colorScheme`
When using @Environment(\.colorScheme) var colorScheme, Xcode prompts: Accessing Environment<ColorScheme>'s value outside of being installed on a View. This will always read the default value and will not update.
Sample code:
struct SamplePopupView: BottomPopup {
@Environment(\.colorScheme) var colorScheme
func configurePopup(popup: BottomPopupConfig) -> BottomPopupConfig {
popup.backgroundColour(colorScheme == .light ? .black : .white)
}
func createContent() -> some View {
// code
}
}
As Xcode suggests, it is not possible to change colors based on a colorScheme.
Is there a way to fix this?
Hey @Kejiasir,
Thanks for sharing this issue. I don't use @Environment values, so I wasn't aware of this problem. I'll try to solve it over the weekend and get back to you with a solution (hopefully).
PS. If you have some spare time, could you confirm that the problem exists in the latest framework version (3.0.0)? Thanks for your help!
Hi @FulcrumOne, thank you for your reply, and thank you for open-sourcing this excellent framework. I am currently using version 2.7.1, so I haven't had time to confirm whether this problem exists in version 3.0.0.
Hey @Kejiasir
I found a way to fix it. I'll implement it this week, till Friday probably.
Wow @FulcrumOne Great, thank you very much.
Hey @Kejiasir,
Quick update - this is a bit more complicated than I initially thought. The problem is that I've implemented it in such a way that the configurePopup() method runs outside of the view context. This causes the problem that when the view refreshes, the function remains intact, which I did as part of the optimisation.
I'm currently considering moving the method to a body declaration, but I need to spend some time thinking about how this will affect the code efficiency.
Another idea is to put Config as a @State value, which will allow it to be updated whenever some value changes (but again, I need to investigate how it will affect the code efficiency).
I'll try my best to finish the task this week.
Have a nice day, Tomasz