i've created a KetupatButton class with 2 method like this:
class KetupatButton {
var condition = false
@State var colorToShow = Color(UIColor(red: 183/255, green: 191/255, blue: 150/255, alpha: 100))
func colorChange() {
if condition == false {
colorToShow = Color(UIColor(red: 183/255, green: 191/255, blue: 150/255, alpha: 100))
} else {
colorToShow = Color(UIColor(red: 19/255, green: 58/255, blue: 27/255, alpha: 100))
}
}
func createButton(size: CGFloat) -> some View {
return AnyView(Button(action: {
self.condition = !self.condition
self.colorChange()
print(self.colorToShow)
print(self.condition)
}, label: {
Rectangle()
.frame(width: size, height: size, alignment: .leading)
.foregroundColor(colorToShow)
}))
}
}
But, when I call that class from my ContentView and tap the button, the button don't change it's color. Even though when i print the colorToShow variable, it changed. But the UI color of the button didn't change...
Here is my ContentView
struct ContentView: View {
var button1 = KetupatButton()
var body: some View {
button1.createButton(size: 200)
}
}
Statedoes not work outside ofView. See several WWDC sessions about SwiftUI concepts, etc.