I'm trying to implement a VStack grid that needs to have a HStack when a condition is met on the next item in a ForEach loop. I have the code below that displays the items in an array but I don't know how to get the next item to check against.
Here's what I have so far.
VStack {
ForEach(item.cards, id: \.self) { item in
switch item.card.size {
case .Large:
LargeCard(card: item.card, viewModel: CardViewModel())
case .Medium:
MediumCard(card: item.card, viewModel: CardViewModel())
case .Small:
HStack(spacing: 16) {
SmallCard(card: item.card, viewModel: CardViewModel())
// This is where I think I need to check and I tried somthing like below, but the compiler crashes
if $0 + 1 < item.cards.count {
if item.card.size == .Small {
SmallCard(card: item.card, viewModel: CardViewModel())
}
}
}
case .none:
Text("No more.")
}
}
}
Here's the item struct:
struct Item: Decodable, Hashable {
let card: Card
}
Here's what I'm wanting to get.