I'm developing the beginning of a card game where players must draw the first N cards and start playing.
My idea is to use a Deck that will handle all these processes giving/removing one or more Cards from owner's hand.
But, since can be a maximum of 6 players, how can it do it?
I was thinking to create one Row on the screen for each player and put here the List<Card> handled by Provider and Consumer.
With this kind of setup what the Deck must do is just to add one or more Cards to the line and it's all over. The problem is that I don't know how Deck can access to the List<Card> inside Provider's Consumer.
I was thinking that Deck should create 6 global keys, passing them to each Consumer so it can access all 6 states but I don't know how to do it, this is what I wrote.
Hand class, handles Cards :
class Hand extends ChangeNotifier {
final List<Card> _hand = [];
void addCard(Card card) {
_hand.add(card);
notifyListeners();
}
void removeAll() {
_hand.clear();
notifyListeners();
}
List<Card> getCards() {
return this._hand;
}
}
GameLineData just uses Provider and Consumer for Hand :
class GameLineData extends StatefulWidget {
Player player;
GameLineData({Key? key, required this.player}) : super(key: key);
@override
State<GameLineData> createState() => _GameLineDataState();
}
class _GameLineDataState extends State<GameLineData> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => Hand(),
child: Consumer<Hand>(
builder: (context, value, child) {
return Row(
children: value.getCards(),
);
},
),
);
}
}
And Deck :
class Deck {
final Map<int, Key> keys = {
1:GlobalKey(),
2:GlobalKey(),
3:GlobalKey(),
4:GlobalKey(),
5:GlobalKey(),
6:GlobalKey(),
};
void giveCards() {
//TODO
}
}
Is my design correct? Any suggestion?