Home > front end >  on change for a button to present a list?
on change for a button to present a list?

Time:03-27

can we use on change for a button to present a list within the same view ? I am a beginner

struct ViewMe: View {
var body: some View {

    Button (action:{
                
            },label:{
                Text("Search")
            })
          // can we do on change here to appear a list
       }

}

CodePudding user response:

According to the information you gave me, here is some code that should work with explanations for each part:

struct ContentView: View {

  //Your variable. @State makes it reload the view when changed.
  @State var listIsShowing = false


  var body: some View {
    VStack {
      //Your Button
      Button (action:{
        //Sets variable to true, showing list
        listIsShowing = true
      },label:{
        Text("Search")
      })

      //To put the button at the top
      Spacer(minLength: 0)

      //if variable that the button changes = true, show list
      if listIsShowing {
        //Your list
        List {
          Text("Your")
          Text("list")
          Text("appears")
          Text("When")
          Text("you")
          Text("click")
          Text("the")
          Text("button")
        }
        //Use below code if you want background to match the top section
        //.listStyle(.plain)
      }
    }
  }
}
  • Related