Created
August 25, 2021 02:50
-
-
Save aheze/d062f9fae42b8c2e62084606e90dcf94 to your computer and use it in GitHub Desktop.
Demo code for "SwiftUI on macOS, how can I make the whole row clickable?"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
@State var selectedSite: String? | |
let sites = ["Gmail", "Apple", "Google"] | |
var body: some View { | |
List { | |
ForEach(sites, id: \.self) { site in | |
Button(action: { | |
self.selectedSite = site | |
}) { | |
// TODO: Make this whole row clickable. | |
HStack { | |
Text(site) | |
Spacer() /// 1. | |
if let selectedSite = self.selectedSite, selectedSite == site { | |
Button(action: {}) { | |
Text("Remove") | |
} | |
.background(Color.black) | |
.foregroundColor(.white) | |
.cornerRadius(6) | |
} | |
} | |
.contentShape(Rectangle()) /// 2. | |
} | |
.frame(maxWidth: .infinity, alignment: .leading) | |
.padding(.horizontal, 10) | |
.padding(.vertical, 8) | |
.buttonStyle(PlainButtonStyle()) | |
.font(.system(size: 15)) | |
.foregroundColor(selectedSite == site ? .white : .black) | |
.background(selectedSite == site ? Color.blue : nil) | |
.cornerRadius(6) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo code for https://stackoverflow.com/q/68915976/14351818