Advertisement
Milotronik

Radio button

Sep 21st, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct RadioButtonView: View {
  4. @State private var selectedRadioButton: Int? = nil
  5. let options: [String]
  6.  
  7. var body: some View {
  8. VStack {
  9. ForEach(options.indices, id: \.self) { index in
  10. ContainerView(
  11. isSelected: Binding(
  12. get: { self.selectedRadioButton == index },
  13. set: { newValue in
  14. if newValue {
  15. self.selectedRadioButton = index
  16. }
  17. }
  18. ),
  19. label: options[index]
  20. )
  21. }
  22. }
  23. }
  24. }
  25.  
  26. struct ContainerView: View {
  27. @Binding var isSelected: Bool
  28. let label: String
  29.  
  30. var body: some View {
  31. HStack {
  32. ThirdPartyRadioButton(isSelected: $isSelected)
  33. Text(label)
  34. }
  35. .padding()
  36. .background(Color.gray.opacity(0.2))
  37. .cornerRadius(8)
  38. }
  39. }
  40.  
  41. // Placeholder for the third-party radio button component
  42. struct ThirdPartyRadioButton: View {
  43. @Binding var isSelected: Bool
  44.  
  45. var body: some View {
  46. Button(action: {
  47. self.isSelected = true
  48. }) {
  49. Image(systemName: isSelected ? "largecircle.fill.circle" : "circle")
  50. .foregroundColor(isSelected ? .blue : .gray)
  51. }
  52. }
  53. }
  54.  
  55. struct ContentView: View {
  56. var body: some View {
  57. RadioButtonView(options: ["Option 1", "Option 2", "Option 3"])
  58. }
  59. }
  60.  
  61. @main
  62. struct MyApp: App {
  63. var body: some Scene {
  64. WindowGroup {
  65. ContentView()
  66. }
  67. }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement