Dynamic content support
fitouch opened this issue · 8 comments
.simpleToast(item: $showToast, options: toastOptions) { HStack { Image(systemName: "exclamationmark.triangle") Text("This is some simple toast message.") } .padding() .background(Color.red.opacity(0.8)) .foregroundColor(Color.white) .cornerRadius(10) }
I need dynamic content support with item in. This doesn't work for me.
.simpleToast(item: $showToast, options: toastOptions) { item in HStack { Image(systemName: "exclamationmark.triangle") Text(item.message) } .padding() .background(Color.red.opacity(0.8)) .foregroundColor(Color.white) .cornerRadius(10) }
@fitouch Why do you need that? You can access the variable showToast directly inside the closure to get your message. The input variable for the closure would only reflect the current value of that variable anyways.
struct ToastContent: Identifiable { var id = UUID() var message: String }
.simpleToast(item: $showToast, options: toastOptions) { Text(showToast?.message) }
withAnimation { showToast = showToast == nil ? ToastContent(message: "Your custom message here") : nil }
Text(showToast?.message)
didn't work
@fitouch Your approach does not work because you need to unwrap optional values before using them in a Text view of SwiftUI. There are several ways to do that. One simple way would be to simply provide a default value:
...
.simpleToast(item: $showToast, options: .init()) {
HStack {
Image(systemName: "exclamationmark.triangle")
Text(showToast?.message ?? "No value message")
}
...
}
@sanzaru that works.
One more thing...I'm using modifierType: .slide, I noticed it slides down from top but didn't slide up when hideAfter: 3, only fades out at the end.
@fitouch I can confirm that the slide modifier is not working anymore as expected. I will have a look at that. Thanks for the info.
@fitouch If you add an animation to your SimpleToastOptions (for example animation: .easeOut) it should fix the problem, for now.
Bug is fixed in version 0.8.1