I'm working on a Custom Container, but I get an error:
Failed to produce diagnostic for expression; please file a bug report
with the following code (error location is at the bottom of the code below):
struct ZoomCardView<Content: View>: View {
let contentDefault: ((Binding<Bool>)) -> Content
let contentZoomed: (Binding<Bool>) -> Content
init(@ViewBuilder defaultContent: @escaping (Binding<Bool>) -> Content, @ViewBuilder zoomed: @escaping (Binding<Bool>) -> Content) {
self.contentDefault = defaultContent
self.contentZoomed = zoomed
}
@State private var isShowing: Bool = false
var body: some View {
HStack {
if isShowing {
self.contentZoomed($isShowing)
.transition(.scale(scale: 0, anchor: .center))
} else {
self.contentDefault($isShowing)
.transition(.scale(scale: 0, anchor: .center))
}
}
}
}
struct DefaultView: View {
var body: some View {
HStack {
Text("Default")
}
}
}
struct ZoomView: View {
var body: some View {
HStack {
Text("Zoom")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View { <----------- *** Error
ZoomCardView { isShowing in
DefaultView()
} zoomed: { isShowing in
ZoomView()
}
.preferredColorScheme(.dark)
}
}
I noticed that in ContentView_Previews
, if I pass the same view, the error disappears. For example:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ZoomCardView { isShowing in
ZoomView()
} zoomed: { isShowing in
ZoomView()
}
.preferredColorScheme(.dark)
}
}
Why can't I pass different views when ZoomCardView
expects a parameter of type <Content: View>
? What I would like to do is to eventually specify a View
, or any of the stack types (i.e. ZStack
, VStack
or HStack
), giving the callsite ultimate control as to what gets to be shown. Thanks in advance!
question from:
https://stackoverflow.com/questions/65929168/why-is-a-custom-container-closure-not-allowing-different-views 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…