MenuPicker Sort when selected SwiftUI

Viewed 657

Im trying to select one of sort in menu and sending backend . But somehow onTapGesture is not triggered. I also tried to use button but it didn't work either. Or is there any other way to trigger any of sort In menu picker.

var sortMenuPicker : some View {
    
    HStack(alignment: .center){
        
        Spacer()
        
        Picker(selection: $selection, label: SortView().frame(height:UIScreen.main.bounds.height * 0.050), content: {
            ForEach(sortListArray, id: \.uid, content: { item in
                ForEach(item.sortList ?? [],id:\.uid) { data in
                    
                    if item.id == "sort" {
                        
                        Text(data.name ?? "")
                            .onTapGesture {
                                
                                sortId = data.id
                                
                                getProductList()
                                
                                
                      }
                            
                    }
                    
                }
                
            })
        })
        .pickerStyle(MenuPickerStyle())
       
        
      
    

enter image description here

1 Answers

Add a tag to your Text() and remove Tap Gesture.

Text(data.name ?? "").tag(data.uid) //<-- Here

And use onReceive to listen to your selection

.onReceive(Just(selection)) { (_) in
    print(selection)
}

Note: for this you need to import Combine

Related