Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a simple Button that opens a Modal when it's clicked. I know the modal can be closed by swiping it down, but I implemented a button to close it and this works.

The only thing that bothers me is that I can't preview my Modal (take a look at my code below)


// Button that opens the modal
struct ContentView: View {
  @State private var willMoveToNextScreen = false
  Button(action: {
          self.willMoveToNextScreen = true
     }, label: {
           Image(systemName: "gear")
                 .font(.system(size: 30))
                 .padding(.leading, 15)
            })
            .sheet(isPresented: $willMoveToNextScreen, content: {
                    SettingsView(willMoveToNextScreen: $willMoveToNextScreen)
     })
}


// Modal
 struct SettingsView: View {
     @Binding var willMoveToNextScreen: Bool
    
      var body: some View {
        VStack{
            HStack {
                Button(action: {
                    self.willMoveToNextScreen = false
                }, label: {
                    Image(systemName: "xmark")
                        .font(.system(size: 30, weight: .semibold))
                })
            }
            .frame(maxWidth: .infinity, alignment: .trailing)
            .padding(15)
            Spacer()
            Text("Aye")
        }
    }
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        // I don't know which value I should provide, it expects a Binding<Bool> value
        SettingsView(willMoveToNextScreen: ??) 
    }
}

willMoveToNextScreen expects a BindingBoolean. How could I fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
421 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use Binding.constant(true). You can use Binding.constant(/*Pass your default value as per type*/) for set data for PreviewProvider.

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView(willMoveToNextScreen: .constant(true)) // Pass true or false
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...