SwiftUI how to dynamically append a view to an existing view

I have an existing ScrollView view with a Picker. Upon selection of a picker value, and user pressing the details button, I want to append a view to the bottom of the existing view. I call detailsView. But it does not seem to work.

    @Environment(\.managedObjectContext) private var viewContext
    @Environment(\.dismiss) var dismiss
    @Environment(\.presentationMode) var presentationMode
    @Binding var chosenProvider: Provider?
    
    @State var selectedLocation: Location?
  
    @State private var vendorNames: [String] = []
    @State private var selectedVendor:String? = nil

    
    @State private var showingSheet = false
    
    @State var state: String?
    @State var zip: String?
    

    var body: some View {
        ScrollView {
            VStack  {
                HStack {
                    Text("Select Vendor")
                    
                    Picker("Provider", selection: $selectedVendor  , content: {
                        ForEach(vendorNames,id: \.self, content: { name in
                            Text(name)
                        })
                    })
                    .pickerStyle(MenuPickerStyle())
                    
                    .onTapGesture {
                        self.getVendorNames() { providers, status in
                            if (status) {
                                vendorNames = providers
                            }
                        }
                    }
                    
                    
                }
                
                .onChange(of: selectedVendor, perform: { newValue in
                    selectedVendor = newValue
                    
                })
                .tint(.orange)
        
                    Button {
                        DetailsView()
                    } label: {
                        Label("Details", systemImage: "list.bullet.rectangle")
                            .foregroundColor(.blue)
                    }
          
                
                Spacer()
            }
        }
            .onAppear{
                self.getVendorNames() { providers, status in
                    if (status) {
                        vendorNames = providers
                    }
                }
            }
            .navigationTitle("Add Vendor")
            .toolbar {
                // Back button
                ToolbarItem(placement: .navigationBarLeading) {
                    Button(action: {  presentationMode.wrappedValue.dismiss() }, label: {
                        HStack(spacing: 2) {
                            Image(systemName: "chevron.backward")
                                .foregroundColor(.black)

                            Button("Back", action: {
                                self.presentationMode.wrappedValue.dismiss()
                            } )
                            .foregroundColor(.black)
                        }
                    })
                }
               
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarBackButtonHidden(true)
    }
  
    struct DetailsView:  View {
        var body: some View {
                VStack  {
                    Text("Some Details")
                }
            }
    }
    
    func getVendorNames (completionHandler: @escaping ([String], Bool) -> Void ) {
      var names = ["Acme", "Abc"]
      
        completionHandler(names, true)
           
    }
}

Replies

DetailsView gets called, but I can't step into it in debug mode .

Don't you get a warning in code?

Result of 'DetailsView' initializer is unused

You should change your Button action:

Button {
    DetailsView()
 } label: {
       Label("Details", systemImage: "list.bullet.rectangle")
              .foregroundColor(.blue)
 }

to

@State var showDetailsView = false 
Button {
     showDetailsView = true
 } label: {
         Label("Details", systemImage: "list.bullet.rectangle")
               .foregroundColor(.blue)
}

if showDetailsView {
  DetailsView()
}