-3

Scenario:

I would like to toggle an entity (UIView)'s property;
like a button's color and perhaps other characteristics per @State boolean var.

Button(action: {
    self.showActionSheet.toggle()
}) {
    Text("Action Sheet")
    .foregroundColor(Color.white)
}
.padding()
.background(Color.blue)// how do I toggle this conditionally: i.e. one of two colors?
.cornerRadius(10)
.shadow(radius: 10)

What's the correct way to do this?

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • 3
    Possible duplicate of [How do you make a Button conditionally hidden or disabled?](https://stackoverflow.com/questions/57420386/how-do-you-make-a-button-conditionally-hidden-or-disabled) Which is your question you accepted the answer on a few minutes ago. Please, if that was an acceptable answer, help everyone nd edit that question. –  Aug 08 '19 at 22:30
  • Please add code, errors and data as **text** ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](https://meta.stackoverflow.com/a/285557). In general, code/errors/data in text format >>>> code/errors/data as an image >> nothing. Images should only be used, *in addition to text in code format*, if having the image adds something significant that is not conveyed by just the text code/error/data. – Makyen Aug 10 '19 at 23:59
  • Replaced image with sample code per feedback. – Frederick C. Lee Aug 12 '19 at 03:15

1 Answers1

4

User the ternary operator:

struct ContentView: View {
    @State private var flag = false

    var body: some View {
        Text("Hello")
            .background(flag ? Color.orange : Color.blue)
            .onTapGesture {
                self.flag.toggle()
            }
    }
}
kontiki
  • 37,663
  • 13
  • 111
  • 125