For every item in an array, I want to load a dynamic gradient of colors that are defined in the JSON. There are different colors in each item.
In the JSON, I'm storing hex values in [String]. I can use these to load Color(hex: String) using the extension from here: Use Hex color in SwiftUI
I was thinking along the following lines for implementation, but Xcode throws all sorts of errors and besides, it feels like a hack. What am I missing?
Example model:
struct Item: Codable, Identifiable, Hashable {
let colors: [String]
}
Example Detailview:
struct DetailView: View {
let item: Item
let gradientColors: [Color]
init(item: Item){
self.item = item
ForEach(item.colors) { color in
gradientColors.append(Color(hex: color))
}
}
var body: some View {
Circle()
.background(LinearGradient(gradient: Gradient(colors: gradientColors), startPoint: .leading, endPoint: .trailing))
}
}