Is it possible to somehow use inheritance with widgets in WidgetKit? I have several widgets that share similar lines of code. How can I create, for example, a generic “circular widget” type with a certain static configuration that I can reuse and further specify across all of my circular widgets?
I tried to create a CircularWidget struct:
struct CircularWidget: Widget {
let kind: String
let name: String
let description: String
let provider: any TimelineProvider
let view: any View
let families = [WidgetFamily.accessoryCircular]
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: provider) { entry in
// WatchOS 10+
if #available(watchOS 10.0, *) {
view
.containerBackground(.background, for: .widget)
}
// Before watchOS 10
else {
view
.padding()
.background()
}
}
.configurationDisplayName(name)
.description(description)
.supportedFamilies(families)
}
}
But it seems to require that I set values for kind, name, description, etc., rather than leaving them undefined. Also, it’s saying that any View cannot conform to View on protocol with generics. I’m not sure how I can leave the view undefined.
Boxbrain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.