title: Why does my StatefulWidget lose its state?
body: <p>I'm building a tinder-like card swipe mechanism where I have a list of cards widget in a <code>Stack</code> widget. The first card is wrapped inside a <code>Dismissible</code> widget which, on dismiss, dismiss the top card and add a new one at the bottom.</p>
  
  <p>My issue here is that, when the first card is dismissed and the second one becomes the first, that card is momentarily disposed and <code>initState</code> is called again</p>
  
  <p>But, as Linus Torvalds once said, </p>
  
  <blockquote>
    <p>"Talk is cheap, show me the code"</p>
  </blockquote>
  
  <p>so I put together a example:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Container(color: Colors.white, child: CardStack()),
      );
    }
  }
  
  class CardStack extends StatefulWidget {
    @override
    _CardStackState createState() =&gt; _CardStackState();
  }
  
  class _CardStackState extends State&lt;CardStack&gt; {
    final List&lt;String&gt; _postIds = ["1", "2", "3"];
    int _nextId = 4;
  
    _updatePosts(DismissDirection dir) {
      setState(() {
        _postIds.removeAt(0);
        _postIds.add((_nextId++).toString());
      });
    }
  
    List&lt;Widget&gt; _buildCards() {
      List&lt;Widget&gt; cards = [];
      for (int i = 0; i &lt; _postIds.length; ++i) {
        if (i == 0) {
          cards.insert(
              0,
              Dismissible(
                key: UniqueKey(),
                onDismissed: _updatePosts,
                child: Card(key: ValueKey(_postIds[i]), postId: _postIds[i]),
              ));
        } else {
          cards.insert(0, Card(key: ValueKey(_postIds[i]), postId: _postIds[i]));
        }
      }
      return cards;
    }
  
    @override
    Widget build(BuildContext context) {
      return Stack(
        alignment: Alignment.center,
        children: _buildCards(),
      );
    }
  }
  
  class Card extends StatefulWidget {
    Card({Key key, this.postId}) : super(key: key);
  
    final String postId;
  
    @override
    _CardState createState() =&gt; _CardState();
  }
  
  class _CardState extends State&lt;Card&gt; {
    @override
    void initState() {
      super.initState();
      print("Init state card with id ${widget.postId}");
    }
  
    @override
    Widget build(BuildContext context) {
      return Container(
        height: 200,
        width: 200,
        color: Colors.blueAccent,
        child: Center(
          child: Text(widget.postId),
        ),
      );
    }
  }
  
  </code></pre>
  
  <p>if I run that it logs</p>
  
  <pre><code>I/flutter (26740): Init state card with id 3
  I/flutter (26740): Init state card with id 2
  I/flutter (26740): Init state card with id 1
  // swipe
  I/flutter (26740): Init state card with id 4
  I/flutter (26740): Init state card with id 2 &lt;- unwanted rebuild
  </code></pre>
  
  <p>I think the rebuild of the first card is due to the fact that is is now wrapped inside the <code>Dismissible</code> widget so flutter doesn't know how to reuse the underlying Card widget.</p>
  
  <p>Is there a way to prevent this unwanted rebuild here?</p>