2023-09-04 19:39:51 +00:00
|
|
|
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(
|
2023-12-25 16:50:58 +00:00
|
|
|
{super.key, required this.coordinate, required this.containedPiece});
|
2023-09-04 19:39:51 +00:00
|
|
|
|
|
|
|
@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;
|
|
|
|
|
2024-01-05 21:59:31 +00:00
|
|
|
if (isPromotionMove(
|
|
|
|
pieceDragged.movedPiece!.pieceClass,
|
|
|
|
ChessBloc.myColor!,
|
|
|
|
pieceDragged.toSquare,
|
|
|
|
)) {
|
2023-09-04 19:39:51 +00:00
|
|
|
var move = ChessMove(
|
|
|
|
from: pieceDragged.fromSquare, to: pieceDragged.toSquare);
|
2024-01-06 12:26:22 +00:00
|
|
|
PromotionBloc().add(PawnMovedToPromotionField(
|
2023-09-04 19:39:51 +00:00
|
|
|
move: move, colorMoved: ChessBloc.myColor!));
|
|
|
|
} else if (coordinate != pieceDragged.fromSquare) {
|
2024-01-06 12:26:22 +00:00
|
|
|
ChessBloc().add(OwnPieceMoved(
|
2023-09-04 19:39:51 +00:00
|
|
|
startSquare: pieceDragged.fromSquare,
|
2024-01-05 21:59:31 +00:00
|
|
|
endSquare: pieceDragged.toSquare));
|
2023-09-04 19:39:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
builder: (context, candidateData, rejectedData) {
|
|
|
|
return ChessSquareInnerDraggable(
|
|
|
|
coordinate: coordinate,
|
|
|
|
containedPiece: containedPiece,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|