2022-11-12 21:55:45 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-11-13 12:09:36 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2022-11-13 02:42:30 +00:00
|
|
|
|
|
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
2022-11-13 00:03:47 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
import 'chess_utils.dart';
|
|
|
|
|
2022-11-13 01:42:10 +00:00
|
|
|
class ChessSquare extends StatelessWidget {
|
2022-11-12 21:55:45 +00:00
|
|
|
final ChessCoordinate coordinate;
|
2022-11-13 12:09:36 +00:00
|
|
|
final ChessPiece? containedPiece;
|
2022-11-12 21:55:45 +00:00
|
|
|
static const double pieceWidth = 200;
|
|
|
|
static const double pieceHeight = 200;
|
|
|
|
|
2022-11-13 12:09:36 +00:00
|
|
|
final Color color;
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2022-11-13 12:09:36 +00:00
|
|
|
const ChessSquare._(
|
|
|
|
{required this.coordinate,
|
|
|
|
required this.containedPiece,
|
2022-11-13 13:28:30 +00:00
|
|
|
required this.color});
|
2022-11-13 12:09:36 +00:00
|
|
|
|
|
|
|
factory ChessSquare(ChessCoordinate coord, ChessPiece? piece) {
|
|
|
|
Color lightSquares = Colors.brown.shade50;
|
|
|
|
Color darkSquares = Colors.brown.shade400;
|
|
|
|
|
|
|
|
Color squareColor;
|
|
|
|
|
|
|
|
if (coord.row % 2 == 0) {
|
|
|
|
if (coord.column % 2 == 0) {
|
|
|
|
squareColor = darkSquares;
|
|
|
|
} else {
|
|
|
|
squareColor = lightSquares;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (coord.column % 2 == 0) {
|
|
|
|
squareColor = lightSquares;
|
|
|
|
} else {
|
|
|
|
squareColor = darkSquares;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChessSquare._(
|
|
|
|
coordinate: coord,
|
|
|
|
containedPiece: piece,
|
|
|
|
color: squareColor,
|
|
|
|
);
|
|
|
|
}
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
double windowWidth = MediaQuery.of(context).size.width;
|
|
|
|
double windowHeight = MediaQuery.of(context).size.height;
|
|
|
|
double draggableFdbSize;
|
|
|
|
|
|
|
|
if (windowWidth < windowHeight) {
|
|
|
|
draggableFdbSize = 0.15 * windowWidth;
|
|
|
|
} else {
|
|
|
|
draggableFdbSize = 0.15 * windowHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DragTarget<ChessMove>(
|
|
|
|
builder: (context, candidateData, rejectedData) {
|
|
|
|
return Container(
|
|
|
|
color: color,
|
|
|
|
width: ChessSquare.pieceWidth,
|
|
|
|
height: ChessSquare.pieceWidth,
|
|
|
|
child: Draggable<ChessMove>(
|
|
|
|
/* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */
|
2022-11-13 01:42:10 +00:00
|
|
|
data: ChessMove(coordinate, coordinate, containedPiece),
|
2022-11-12 21:55:45 +00:00
|
|
|
feedback: FractionalTranslation(
|
|
|
|
translation: const Offset(-0.5, -0.75),
|
|
|
|
child: SizedBox(
|
|
|
|
height: draggableFdbSize,
|
|
|
|
width: draggableFdbSize,
|
2022-11-13 01:42:10 +00:00
|
|
|
child: containedPiece),
|
2022-11-12 21:55:45 +00:00
|
|
|
),
|
|
|
|
childWhenDragging: Container(),
|
|
|
|
dragAnchorStrategy: pointerDragAnchorStrategy,
|
2022-11-13 01:42:10 +00:00
|
|
|
child: containedPiece ?? Container(),
|
|
|
|
onDragCompleted: () {},
|
|
|
|
onDragStarted: () {},
|
2022-11-12 21:55:45 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onWillAccept: (move) {
|
2022-11-13 12:09:36 +00:00
|
|
|
final legalMove = context
|
|
|
|
.read<ChessBloc>()
|
|
|
|
.preCheckHandler(PreCheckMove(move: move!));
|
2022-11-13 02:50:29 +00:00
|
|
|
print('onWillAccept returns $legalMove');
|
|
|
|
return legalMove;
|
2022-11-13 00:03:47 +00:00
|
|
|
},
|
|
|
|
onAccept: (move) {
|
2022-11-13 01:42:10 +00:00
|
|
|
move.endSquare = coordinate;
|
2022-11-13 02:24:42 +00:00
|
|
|
|
|
|
|
if (move.endSquare != move.startSquare) {
|
2022-11-13 12:09:36 +00:00
|
|
|
context.read<ChessBloc>().add(
|
|
|
|
PieceMoved(
|
|
|
|
startSquare: move.startSquare,
|
|
|
|
endSquare: move.endSquare,
|
|
|
|
),
|
|
|
|
);
|
2022-11-13 02:24:42 +00:00
|
|
|
}
|
2022-11-12 21:55:45 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|