I have a Stack of two custom widgets, which both are Positioned:
- ProfileCard
- ProfileCardDummy
ProfileCard: This custom Stateful Widget is the widget of the first stack, the stack on the top. This widget ris Dismissible.
ProfileCardDummy: This custom Stateless widget is the background widgets of the stack.
So ProfileCard widget is the one on the top of the Stack, and the other background stacks are ProfileCardDummy
My Code is something like that:
return Stack(
alignment: AlignmentDirectional.center,
children:
state.profileCards.map((ProfileCardModel card) {
backCardWidth += 10.0;
backBottom -= decrementalBottom;
if (state.profileCards.indexOf(card) ==
state.profileCards.length - 1) {
return BlocProvider < ProfileCardBloc > (
bloc: _profileCardBloc,
child: ProfileCard(
isBack: 0,
bottom: initialBottom,
cardModel: card,
rotation: 0.0,
skew: 0.0,
img: image1,
addCard: () {},
onPressed: () {},
),
);
} else {
return ProfileCardDummy(
card,
image2,
backBottom,
backCardWidth,
);
}
}).toList()
);
All the magic happens in ProfileCard, where I'm fetching content of the card (mainly buttons) using BloC pattern.
I'm dispatching the bloc event on the constructor of the ProfileCard state. Something like:
class _ProfileCardState extends State<ProfileCard> {
final VoteButtonBloc _voteButtonBloc = VoteButtonBloc();
_ProfileCardState(){
print("Dispatching ${widget.cardModel}");
_voteButtonBloc.dispatch(FetchButtons(widget.cardModel));
}
...
...
Problem is, that when I run the code, the first ProfileCard shows its buttons correctly. Now when I dismiss it, the second ProfileCard shows, but with the buttons of the first ProfileCard (the one I dismissed).
After hours of debugging, I noticed that the constructor of the ProfileCard (_ProfileCardState()) is being called only on the first card. Other cards are inheriting the buttons of the first class.
In the documentation of Stack, it says:
If you reorder the children in this way, consider giving the children non-null keys.
These keys will cause the framework to move the underlying objects for the children to their new locations rather than recreate them at their new location.
My ProfileCard takes a key as argument (as it's extending StatefulWidget), but I'm not passing any key. Passing a Key would solve this? How to pass a unique key?:P
Is there a method to force the recreation of the widget to force the constructor being called again, and thus fetching the buttons correctly?