我想通过将函数传递给 .background() 来更改文本背景颜色,如下所示,颜色的值为 0 或 1 或 2 将取决于数据库的数据。我该如何修复它:
import SwiftUI
struct ContentView: View {
@State var color = 0;
var body: some View {
Text("Hello, World!")
.background(changeBkColor(int : self.$color))
}
}
func changeBkColor(int : color)
{
if(color == 1)
{
return Color.red;
}
else if(color == 2)
{
return Color.green;
}
else
{
return Color.blue;
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)