'App freeze when tap on textfield in conditional section

I have a textfield that can be modified programatically. It works fine, until the form section is conditionally displayed, in that case the app freezes when I use the textfield.

Here is my code (I removed unnecessary stuff):

struct test: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var displaySection = true
    @State private var amount: Double?
    @FocusState private var isFocused: Bool
    
    var body: some View {
        NavigationView{
            Form {
                if displaySection {     // <-- works well without that condition, otherwise app freezes
                    Section {
                        VStack {
                            HStack {
                                Text("Amount EUR")
                                Spacer()
                                TextField("Type amount", value: $amount, format: .number)
                                    .keyboardType(.numberPad)
                                    .multilineTextAlignment(.trailing)
                                    .focused($isFocused)
                            }
                            Text("Set MAX (999)")
                                .frame(maxWidth: .infinity, alignment: .leading)
                                .onTapGesture {
                                    isFocused = false
                                    amount = 999
                                }
                        }
                    }
                }
            }
        }
    }
}

I need to have some sections hidden in the form so I'm stuck on that, didn't succeed to skirt this issue. Problem appears on simulator as well on device.

Is this behavior normal, or is there a possible workaround ? Thanks for your help :)



Solution 1:[1]

Divide and conquer.

You presented a really weird behaviour in your app, I would never imagine such bug. I tested your code, it actually freezes the app.

Now, I tested the solution below: move your Section to a separate view. In my Xcode, it works.

struct Example: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var displaySection = true
    
    var body: some View {
        NavigationView{
            Form {
                if displaySection {     // <-- works well ALSO WITH that condition
                    ConditionalSection()
                }
                
                Button {
                    withAnimation {
                        displaySection.toggle()
                    }
                } label: {
                    Text("Display or not display?")
                }
            }
        }
    }
}

struct ConditionalSection: View {
    @State private var amount: Double?
    @FocusState private var isFocused: Bool
    
    var body: some View {
        Section {
            VStack {
                HStack {
                    Text("Amount EUR")
                    Spacer()
                    TextField("Type amount", value: $amount, format: .number)
                        .keyboardType(.numberPad)
                        .multilineTextAlignment(.trailing)
                        .focused($isFocused)
                }
                Text("Set MAX (999)")
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .onTapGesture {
                        isFocused = false
                        amount = 999
                    }
            }
        }

    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HunterLion