I will use the previous answer of @LizJ and i will add a text after the radio button to resemble (RadioListTile in Flutter)
struct RadioButton: View { let ifVariable: Bool //the variable that determines if its checked let radioTitle: String var onTapToActive: ()-> Void//action when taped to activate let onTapToInactive: ()-> Void //action when taped to inactivate var body: some View { Group{ if ifVariable { HStack(alignment: .center, spacing: 16) { ZStack{ Circle() .fill(AppColors.primaryColor) .frame(width: 20, height: 20) Circle() .fill(Color.white) .frame(width: 8, height: 8) }.onTapGesture {self.onTapToInactive()} Text(radioTitle) .font(.headline) } } else { HStack(alignment: .center, spacing: 16){ Circle() .fill(Color.white) .frame(width: 20, height: 20) .overlay(Circle().stroke(Color.gray, lineWidth: 1)) .onTapGesture {self.onTapToActive()} Text(radioTitle) .font(.headline) } } } }
I will also provide an example for the selection logic we will create a enum for radio cases
enum PaymentMethod: Int { case undefined = 0 case credit = 1 case cash = 2 }
then we will create @State variable to carry the selection, i will not recreate another SwiftUI view but only explain the basic concept without any boilerplate code
struct YourView: View { @State private var paymentMethod: PaymentMethod var body: some View { RadioButton(ifVariable: paymentMethod == PaymentMethod.credit,radioTitle: "Pay in Credit", onTapToActive: { paymentMethod = .credit }, onTapToInactive: {}) RadioButton(ifVariable: paymentMethod == PaymentMethod.cash,radioTitle: "Pay in Cash", onTapToActive: { paymentMethod = .cash }, onTapToInactive: {}) } }
with this previous code you can toggle between radio buttons in SwiftUI with a text after each selection to resemble (RadioListTile in Flutter)