73 lines
2.2 KiB
Dart
73 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mchess/chess/chess_square_inner_draggable.dart';
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
|
import 'package:mchess/chess_bloc/promotion_bloc.dart';
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
|
|
class ChessSquareOuterDragTarget extends StatelessWidget {
|
|
final ChessCoordinate coordinate;
|
|
final ChessPiece containedPiece;
|
|
|
|
const ChessSquareOuterDragTarget(
|
|
{super.key, required this.coordinate, required this.containedPiece});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return DragTarget<PieceDragged>(
|
|
onWillAccept: (move) {
|
|
if (move?.fromSquare == coordinate) {
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
onAccept: (pieceDragged) {
|
|
// Replace the dummy value with the actual target of the move.
|
|
pieceDragged.toSquare = coordinate;
|
|
|
|
if (isPromotionMove(pieceDragged)) {
|
|
var move = ChessMove(
|
|
from: pieceDragged.fromSquare, to: pieceDragged.toSquare);
|
|
PromotionBloc.getInstance().add(PawnMovedToPromotionField(
|
|
move: move, colorMoved: ChessBloc.myColor!));
|
|
} else if (coordinate != pieceDragged.fromSquare) {
|
|
ChessBloc.getInstance().add(OwnPieceMoved(
|
|
startSquare: pieceDragged.fromSquare,
|
|
endSquare: pieceDragged.toSquare,
|
|
piece: pieceDragged.movedPiece!));
|
|
}
|
|
},
|
|
builder: (context, candidateData, rejectedData) {
|
|
return ChessSquareInnerDraggable(
|
|
coordinate: coordinate,
|
|
containedPiece: containedPiece,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
bool isPromotionMove(PieceDragged move) {
|
|
bool isPromotion = false;
|
|
if (move.movedPiece!.pieceClass != ChessPieceClass.pawn) {
|
|
return isPromotion;
|
|
}
|
|
|
|
switch (ChessBloc.myColor) {
|
|
case ChessColor.black:
|
|
if (move.toSquare.row == 1) {
|
|
isPromotion = true;
|
|
}
|
|
break;
|
|
case ChessColor.white:
|
|
if (move.toSquare.row == 8) {
|
|
isPromotion = true;
|
|
}
|
|
break;
|
|
case null:
|
|
break;
|
|
}
|
|
|
|
return isPromotion;
|
|
}
|
|
}
|