'SwiftUi dragging: change color of containing view from dropEntered

I have a list of 3 vertical views. When I drag an item over another one, I want the target item (as defined by dropEntered) to change its bg color. Problem of my code is that it changes the colors of ALL items of the view:

struct HomeTabView: View {
    
    @State var items = ["1", "2", "3"]
    @State var draggedItem : String?
    @State var backgroundColor: Color = .red
    
    var body: some View {
        LazyVStack(spacing : 15) {
            ForEach(items, id:\.self) { item in
                Text(item)
                    .frame(minWidth:0, maxWidth:.infinity, minHeight:50)
                    .border(Color.black)
                    .background(backgroundColor)
                    .onDrag({
                        self.draggedItem = item
                        return NSItemProvider(item: nil, typeIdentifier: item)
                    }) .onDrop(of: [UTType.text], delegate: MyDropDelegate(color: $backgroundColor, item: item, items: $items, draggedItem: $draggedItem ))
            }
        }
    }
}

Drop delegate:

struct MyDropDelegate : DropDelegate {
    @Binding var color: Color

    
    var item : String
    @Binding var items : [String]
    @Binding var draggedItem : String?
    
    func dropEntered(info: DropInfo) {
        
        if draggedItem != item {
            self.color = .blue
        }
    }
    
    func dropExited(info: DropInfo) {
        
        if draggedItem != item {
            self.color = .red
        }
    }
    
    func performDrop(info: DropInfo) -> Bool {
        return true
    }
   } 

as you can see self.color = .blue changes the color of all the views and not the targeted one. If I try to modify "item" it won't work because it's only a string and not the entire contained view.

So how to change this code so I can change bg color of item designated by dropEntered?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source