I'm building a tinder-like card swipe mechanism where I have a list of cards widget in a Stack
widget. The first card is wrapped inside a Dismissible
widget which, on dismiss, dismiss the top card and add a new one at the bottom.
My issue here is that, when the first card is dismissed and the second one becomes the first, that card is momentarily disposed and initState
is called again
But, as Linus Torvalds once said,
"Talk is cheap, show me the code"
so I put together a example:
import 'package:flutter/material.dart';
void main() => 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() => _CardStackState();
}
class _CardStackState extends State<CardStack> {
final List<String> _postIds = ["1", "2", "3"];
int _nextId = 4;
_updatePosts(DismissDirection dir) {
setState(() {
_postIds.removeAt(0);
_postIds.add((_nextId++).toString());
});
}
List<Widget> _buildCards() {
List<Widget> cards = [];
for (int i = 0; i < _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() => _CardState();
}
class _CardState extends State<Card> {
@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),
),
);
}
}
if I run that it logs
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 <- unwanted rebuild
I think the rebuild of the first card is due to the fact that is is now wrapped inside the Dismissible
widget so flutter doesn't know how to reuse the underlying Card widget.
Is there a way to prevent this unwanted rebuild here?