Last active
January 19, 2019 09:35
-
-
Save deven98/6edc2c841a628b43db3a20bcafffac3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:solitaire_flutter/playing_card.dart'; | |
import 'package:solitaire_flutter/transformed_card.dart'; | |
typedef Null CardAcceptCallback(List<PlayingCard> card, int fromIndex); | |
// This is a stack of overlayed cards (implemented using a stack) | |
class CardColumn extends StatefulWidget { | |
// List of cards in the stack | |
final List<PlayingCard> cards; | |
// Callback when card is added to the stack | |
final CardAcceptCallback onCardsAdded; | |
// The index of the list in the game | |
final int columnIndex; | |
CardColumn( | |
{@required this.cards, | |
@required this.onCardsAdded, | |
@required this.columnIndex}); | |
@override | |
_CardColumnState createState() => _CardColumnState(); | |
} | |
class _CardColumnState extends State<CardColumn> { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: 13.0 * 15.0, | |
width: 70.0, | |
margin: EdgeInsets.all(2.0), | |
child: DragTarget<Map>( | |
builder: (context, listOne, listTwo) { | |
return Stack( | |
children: widget.cards.map((card) { | |
int index = widget.cards.indexOf(card); | |
return TransformedCard( | |
playingCard: card, | |
transformIndex: index, | |
attachedCards: widget.cards.sublist(index, widget.cards.length), | |
columnIndex: widget.columnIndex, | |
); | |
}).toList(), | |
); | |
}, | |
onWillAccept: (value) { | |
// If empty, accept | |
if (widget.cards.length == 0) { | |
return true; | |
} | |
// Get dragged cards list | |
List<PlayingCard> draggedCards = value["cards"]; | |
PlayingCard firstCard = draggedCards.first; | |
if (firstCard.cardColor == CardColor.red) { | |
if (widget.cards.last.cardColor == CardColor.red) { | |
return false; | |
} | |
int lastColumnCardIndex = CardType.values.indexOf(widget.cards.last.cardType); | |
int firstDraggedCardIndex = CardType.values.indexOf(firstCard.cardType); | |
if(lastColumnCardIndex != firstDraggedCardIndex + 1) { | |
return false; | |
} | |
} else { | |
if (widget.cards.last.cardColor == CardColor.black) { | |
return false; | |
} | |
int lastColumnCardIndex = CardType.values.indexOf(widget.cards.last.cardType); | |
int firstDraggedCardIndex = CardType.values.indexOf(firstCard.cardType); | |
if(lastColumnCardIndex != firstDraggedCardIndex + 1) { | |
return false; | |
} | |
} | |
return true; | |
}, | |
onAccept: (value) { | |
widget.onCardsAdded( | |
value["cards"], | |
value["fromIndex"], | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment