title: SwiftUI add subview dynamically but the animation doesn't work
body: <p>I would like to create a view in SwiftUI that add a subview dynamically and with animation.</p>
  
  <pre><code>struct ContentView : View {
      @State private var isButtonVisible = false
  
      var body: some View {
          VStack {
              Toggle(isOn: $isButtonVisible.animation()) {
                  Text("add view button")
              }
  
             if isButtonVisible {
                   AnyView(DetailView())
                        .transition(.move(edge: .trailing))
                        .animation(Animation.linear(duration: 2))
               }else{
                      AnyView(Text("test"))
              }
          }
      }
  }
  </code></pre>
  
  <p>The above code works fine with the animation . however when i move the view selection part into a function, the animation is not working anymore (since i want to add different views dynamically, therefore, I put the logic in a function.)</p>
  
  <pre><code>
  struct ContentView : View {
      @State private var isButtonVisible = false
      var body: some View {
          VStack {
              Toggle(isOn: $isButtonVisible.animation()) {
                  Text("add view button")
              }
  
              subView().transition(.move(edge: .trailing))
                       .animation(Animation.linear(duration: 2))   
      }
  
       func subView() -&gt; some View {
           if isButtonVisible {
               return AnyView(DetailView())
           }else{
              return AnyView(Text("test"))
          }
      }
  }
  </code></pre>
  
  <p>it looks totally the same to me, however, i don't understand why they have different result. Could somebody explain me why? and any better solutions?  thanks alot!</p>