How can the order of a VStack be changed during runtime? For HStack, the layout direction can be changed from .leftToRight and .rightToLeft. With ZStack, a zIndex can be adjusted to change order of the views, front to back. Is there something that can be used to change the order of a VStack? In my project, reversing the order top-to-bottom would work.
This is a greatly simplified example with duplication:
struct ContentView: View {
@State var shouldFlip = false
var body: some View {
if shouldFlip {
VStack {
Color(.red)
Color(.blue)
Color(.yellow)
Color(.green)
}
} else {
VStack {
Color(.green)
Color(.yellow)
Color(.blue)
Color(.red)
}
}
}
}
To avoid duplication is there anything like the following but for a VStack with .topToBottom and .bottomToTop:
struct ContentView: View {
@State var shouldFlip = true
var body: some View {
HStack {
Color(.red)
Color(.blue)
Color(.yellow)
Color(.green)
}.environment(\.layoutDirection, shouldFlip ? .rightToLeft : .leftToRight)
}
}