mchess-client/lib/chess/chess_square_outer_dragtarget.dart

55 lines
1.8 KiB
Dart
Raw Permalink Normal View History

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/chess_bloc/tap_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});
@override
Widget build(BuildContext context) {
return DragTarget<PieceDragged>(
onWillAcceptWithDetails: (details) {
if (details.data.fromSquare == coordinate) {
return false;
}
return true;
},
onAcceptWithDetails: (details) {
// Replace the dummy value with the actual target of the move.
details.data.toSquare = coordinate;
TapBloc().add(CancelTapEvent());
if (isPromotionMove(
details.data.movedPiece!.pieceClass,
ChessBloc.getMyColor(),
details.data.toSquare,
)) {
var move = ChessMove(
from: details.data.fromSquare, to: details.data.toSquare);
PromotionBloc().add(PawnMovedToPromotionField(
move: move, colorMoved: ChessBloc.myColor));
} else if (coordinate != details.data.fromSquare) {
ChessBloc().add(OwnPieceMoved(
startSquare: details.data.fromSquare,
endSquare: details.data.toSquare));
}
},
builder: (context, candidateData, rejectedData) {
return ChessSquareInnerDraggable(
coordinate: coordinate,
containedPiece: containedPiece,
);
},
);
}
}