mchess-client/lib/chess/chess_square_outer_dragtarget.dart
Marco 6882505174 Start a tap with drag
Now, only the empty squares contain a GestureDetector.
The GestureDetector is not necessary anymore in squares with pieces
because the tap is started when the drag is started.
2024-02-01 11:11:11 +01:00

55 lines
1.8 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/chess_bloc/tap_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>(
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.myColor!,
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,
);
},
);
}
}