2022-11-12 21:55:45 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-09-04 19:39:51 +00:00
|
|
|
import 'package:mchess/chess/chess_square_outer_dragtarget.dart';
|
2024-02-01 10:37:00 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
2024-01-05 21:59:31 +00:00
|
|
|
import 'package:mchess/chess_bloc/tap_bloc.dart';
|
2022-12-25 15:16:23 +00:00
|
|
|
import '../utils/chess_utils.dart';
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2023-09-04 19:52:40 +00:00
|
|
|
class ChessSquare extends StatefulWidget {
|
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
|
|
|
|
2023-08-19 01:45:03 +00:00
|
|
|
const ChessSquare._({
|
|
|
|
required this.coordinate,
|
|
|
|
required this.containedPiece,
|
|
|
|
required this.color,
|
|
|
|
});
|
|
|
|
|
2024-01-05 21:59:31 +00:00
|
|
|
factory ChessSquare(ChessCoordinate coord, ChessPiece? piece,
|
|
|
|
bool wasPartOfLastMove, bool wasTapped) {
|
2023-08-19 01:45:03 +00:00
|
|
|
Color lightSquaresColor =
|
|
|
|
wasPartOfLastMove ? Colors.green.shade200 : Colors.brown.shade50;
|
|
|
|
Color darkSquaresColor =
|
|
|
|
wasPartOfLastMove ? Colors.green.shade300 : Colors.brown.shade400;
|
2022-11-13 12:09:36 +00:00
|
|
|
|
2024-01-05 21:59:31 +00:00
|
|
|
if (wasTapped) {
|
|
|
|
lightSquaresColor = Colors.red.shade200;
|
|
|
|
darkSquaresColor = Colors.red.shade300;
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:09:36 +00:00
|
|
|
Color squareColor;
|
|
|
|
|
|
|
|
if (coord.row % 2 == 0) {
|
|
|
|
if (coord.column % 2 == 0) {
|
2023-08-19 01:45:03 +00:00
|
|
|
squareColor = darkSquaresColor;
|
2022-11-13 12:09:36 +00:00
|
|
|
} else {
|
2023-08-19 01:45:03 +00:00
|
|
|
squareColor = lightSquaresColor;
|
2022-11-13 12:09:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (coord.column % 2 == 0) {
|
2023-08-19 01:45:03 +00:00
|
|
|
squareColor = lightSquaresColor;
|
2022-11-13 12:09:36 +00:00
|
|
|
} else {
|
2023-08-19 01:45:03 +00:00
|
|
|
squareColor = darkSquaresColor;
|
2022-11-13 12:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChessSquare._(
|
|
|
|
coordinate: coord,
|
|
|
|
containedPiece: piece,
|
|
|
|
color: squareColor,
|
|
|
|
);
|
|
|
|
}
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2023-09-04 19:52:40 +00:00
|
|
|
@override
|
|
|
|
State<ChessSquare> createState() => _ChessSquareState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChessSquareState extends State<ChessSquare> {
|
2022-11-12 21:55:45 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-02-01 10:11:11 +00:00
|
|
|
var dragTarget = Container(
|
|
|
|
color: widget.color,
|
|
|
|
child: ChessSquareOuterDragTarget(
|
|
|
|
coordinate: widget.coordinate,
|
|
|
|
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
|
2022-11-12 21:55:45 +00:00
|
|
|
);
|
2024-02-01 10:11:11 +00:00
|
|
|
|
2024-02-01 10:37:00 +00:00
|
|
|
if (widget.containedPiece == null ||
|
|
|
|
widget.containedPiece!.color != ChessBloc.getMyColor()) {
|
2024-02-01 10:11:11 +00:00
|
|
|
return GestureDetector(
|
|
|
|
child: dragTarget,
|
|
|
|
onTap: () {
|
|
|
|
TapBloc().add(SquareTappedEvent(
|
|
|
|
tapped: widget.coordinate, pieceOnSquare: widget.containedPiece));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return dragTarget;
|
|
|
|
}
|
2022-11-12 21:55:45 +00:00
|
|
|
}
|
|
|
|
}
|