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';
|
2024-02-01 10:11:11 +00:00
|
|
|
import 'package:mchess/chess_bloc/tap_bloc.dart';
|
2023-09-04 19:39:51 +00:00
|
|
|
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>(
|
2024-02-01 10:11:11 +00:00
|
|
|
onWillAcceptWithDetails: (details) {
|
|
|
|
if (details.data.fromSquare == coordinate) {
|
2023-09-04 19:39:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2024-02-01 10:11:11 +00:00
|
|
|
onAcceptWithDetails: (details) {
|
2023-09-04 19:39:51 +00:00
|
|
|
// Replace the dummy value with the actual target of the move.
|
2024-02-01 10:11:11 +00:00
|
|
|
details.data.toSquare = coordinate;
|
|
|
|
|
|
|
|
TapBloc().add(CancelTapEvent());
|
2023-09-04 19:39:51 +00:00
|
|
|
|
2024-01-05 21:59:31 +00:00
|
|
|
if (isPromotionMove(
|
2024-02-01 10:11:11 +00:00
|
|
|
details.data.movedPiece!.pieceClass,
|
2024-02-01 10:37:00 +00:00
|
|
|
ChessBloc.getMyColor(),
|
2024-02-01 10:11:11 +00:00
|
|
|
details.data.toSquare,
|
2024-01-05 21:59:31 +00:00
|
|
|
)) {
|
2023-09-04 19:39:51 +00:00
|
|
|
var move = ChessMove(
|
2024-02-01 10:11:11 +00:00
|
|
|
from: details.data.fromSquare, to: details.data.toSquare);
|
2024-01-05 21:59:31 +00:00
|
|
|
PromotionBloc().add(PawnMovedToPromotionField(
|
2024-02-01 10:37:00 +00:00
|
|
|
move: move, colorMoved: ChessBloc.myColor));
|
2024-02-01 10:11:11 +00:00
|
|
|
} else if (coordinate != details.data.fromSquare) {
|
2024-01-05 21:59:31 +00:00
|
|
|
ChessBloc().add(OwnPieceMoved(
|
2024-02-01 10:11:11 +00:00
|
|
|
startSquare: details.data.fromSquare,
|
|
|
|
endSquare: details.data.toSquare));
|
2023-09-04 19:39:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
builder: (context, candidateData, rejectedData) {
|
|
|
|
return ChessSquareInnerDraggable(
|
|
|
|
coordinate: coordinate,
|
|
|
|
containedPiece: containedPiece,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|