0

Tried to do a func but having issues returning "some view". I'm trying to do a while loop with a continue statement as well but that's not allowed within the body of the viewBuilder... what's the easiest way to use pickers when conditions change?

var body: some View {
    VStack{
        Group{
            Form {
                Section(header: Text("\(screeningTable())")) {
                    Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) {
                        List(maleDataModel.ageArray, id: \.self) { i in
                            Text("\(i, specifier: "%g")-years-old")
                        }
                    }

                if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 {
                    Section {
                        Text("Fill all required fields").foregroundColor(.red)
                    }
                }

                if screeningTable() == "No Go" {
                    Section(header: Text("Tape Mesurements")) {
                        MaleTapeView()
                    }
                }
                if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"{
                    Section(header: Text("You are not required to tape").foregroundColor(.blue)) {
                        MaleSaveButton()
                    }
                }
            }
        }
cbear84
  • 486
  • 4
  • 13
  • check this answer here https://stackoverflow.com/questions/58676483/is-there-a-way-to-call-a-function-when-a-swiftui-picker-selection-changes – Chris Nov 24 '19 at 19:36

2 Answers2

0

I am not sure if your code is valid code, but if you want if statements, what could be done is enclosing them in ZStacks

var body: some View {
    VStack{
        Group{
            Form {
                Section(header: Text("\(screeningTable())")) {
                    Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) {
                        List(maleDataModel.ageArray, id: \.self) { i in
                            Text("\(i, specifier: "%g")-years-old")
                        }
                    }
                ZStack {
                    if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 {
                        Section {
                            Text("Fill all required fields").foregroundColor(.red)
                        }
                    }
                }
                ZStack {
                    if screeningTable() == "No Go" {
                        Section(header: Text("Tape Mesurements")) {
                            MaleTapeView()
                        }
                    }
                }
                ZStack {
                    if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"{
                        Section(header: Text("You are not required to tape").foregroundColor(.blue)) {
                        MaleSaveButton()
                    }
                }
            }
        }
    }
0

I got this from the Design + Code course. I can see how the @State property can toggle back and forth without the endless conditional statements...

            BottomCardView(showRing: $showCard)
            .offset(y: showCard ? screen.height/2 - 50 : screen.height)
            .offset(y: bottomState.height)
            .blur(radius: show ? 20 : 0)
            .animation(.spring(response: 0.5, dampingFraction: 0.7, blendDuration: 0))
            .gesture(
                DragGesture()
                    .onChanged { value in
                        self.bottomState = value.translation
                        if self.showFull {
                            self.bottomState.height += -300
                        }
                        if self.bottomState.height < -300 {
                            self.bottomState.height = -300
                        }
                }
                .onEnded { value in
                    if self.bottomState.height > 50 {
                        self.showCard = false
                    }
                    if (self.bottomState.height < -100 && !self.showFull) || (self.bottomState.height < -250 && self.showFull) {
                        self.bottomState.height = -300
                        self.showFull = true
                    } else {
                        self.bottomState = .zero
                        self.showFull = false
                    }
                }
        )
cbear84
  • 486
  • 4
  • 13