'Perform task only when View appear on screen

I have made this demo app

struct TestView: View {

@State var flag = false

var body: some View {
    NavigationView {
        ScrollView {
            VStack{
                ForEach((1...50), id: \.self){ item in
                    Text("\(item)")
                }
                ProgressView()
                    .onAppear {
                        flag = true
                    }
            }
        }
        .navigationTitle(Text(flag == true ? "True" : "False"))
    }
}
}

I wanted to achieve that only when ProgressView() has been seen, then app title should be changed.

But problem is that ProgressView is immediately changing flag value.

Is it possible to make that only when View is on screen, some code can perform?



Solution 1:[1]

Answer to this question is using LazyVStack instead of VStack.

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 Marko Markovic