jeudi 2 novembre 2023

Unable and Disable CheckBox in Swift UI on Toggle

I want if user check the checkbox one then other one should be disable , currently user can select both.

I have seen some example but here I need to have bool values into modal struct but Please note that I do not have bool value into Model . Can I have do it with hardcoded values ?

Here is my CheckboxToggleStyle view code ..

import SwiftUI

struct CheckboxToggleStyle: ToggleStyle {
    @Environment(\.isEnabled) var isEnabled
    let style: Style // custom param

    func makeBody(configuration: Configuration) -> some View {
        Button(action: {
            configuration.isOn.toggle() // toggle the state binding
        }, label: {
            HStack {
                Image(systemName: configuration.isOn ? "checkmark.\(style.sfSymbolName).fill" : style.sfSymbolName)
                    .imageScale(.large)
                configuration.label
            }
        })
        .buttonStyle(PlainButtonStyle()) // remove any implicit styling from the button
        .disabled(!isEnabled)
    }

    enum Style {
        case square, circle

        var sfSymbolName: String {
            switch self {
            case .square:
                return "square"
            case .circle:
                return "circle"
            }
        }
    }
}

Here is the view code ...

struct OrderView: View {
    private var fee: Double = 5.00
    @State private var isOn1 = false
    @State private var isOn2 = false

    var body: some View {
                      VStack(alignment: .leading) {
                        Toggle("Standard : Free ", isOn: $isOn1)
                            .toggleStyle(CheckboxToggleStyle(style: .circle))
                            .foregroundColor(.blue)
                            .onTapGesture {
                                //
                            }

                        Toggle("Express : Charge \(fee.formatted(.currency(code: "GBP")))", isOn: $isOn2)
                            .toggleStyle(CheckboxToggleStyle(style: .circle))
                            .foregroundColor(.blue)
                            .onTapGesture {
                                //

                            }
                    }
                }



Here is the screenshot.. when I select on checkbox other one should be disable but is not..

enter image description here




Aucun commentaire:

Enregistrer un commentaire