mchess-client/lib/chess/chess_square.dart
Marco 200393ac76 Fix tapping on opponent's piece
With the previous change of starting a tap on a drag,
we removed the GestureDetector from all squares with any pieces.

This included the opponent's pieces. Tapping and taking an opponent's
piece was not possible anymore.

This was a bug, since we still want to detect a tap and take an opponent's piece.
2024-02-01 11:37:00 +01:00

84 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mchess/chess/chess_square_outer_dragtarget.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/tap_bloc.dart';
import '../utils/chess_utils.dart';
class ChessSquare extends StatefulWidget {
final ChessCoordinate coordinate;
final ChessPiece? containedPiece;
static const double pieceWidth = 200;
static const double pieceHeight = 200;
final Color color;
const ChessSquare._({
required this.coordinate,
required this.containedPiece,
required this.color,
});
factory ChessSquare(ChessCoordinate coord, ChessPiece? piece,
bool wasPartOfLastMove, bool wasTapped) {
Color lightSquaresColor =
wasPartOfLastMove ? Colors.green.shade200 : Colors.brown.shade50;
Color darkSquaresColor =
wasPartOfLastMove ? Colors.green.shade300 : Colors.brown.shade400;
if (wasTapped) {
lightSquaresColor = Colors.red.shade200;
darkSquaresColor = Colors.red.shade300;
}
Color squareColor;
if (coord.row % 2 == 0) {
if (coord.column % 2 == 0) {
squareColor = darkSquaresColor;
} else {
squareColor = lightSquaresColor;
}
} else {
if (coord.column % 2 == 0) {
squareColor = lightSquaresColor;
} else {
squareColor = darkSquaresColor;
}
}
return ChessSquare._(
coordinate: coord,
containedPiece: piece,
color: squareColor,
);
}
@override
State<ChessSquare> createState() => _ChessSquareState();
}
class _ChessSquareState extends State<ChessSquare> {
@override
Widget build(BuildContext context) {
var dragTarget = Container(
color: widget.color,
child: ChessSquareOuterDragTarget(
coordinate: widget.coordinate,
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
);
if (widget.containedPiece == null ||
widget.containedPiece!.color != ChessBloc.getMyColor()) {
return GestureDetector(
child: dragTarget,
onTap: () {
TapBloc().add(SquareTappedEvent(
tapped: widget.coordinate, pieceOnSquare: widget.containedPiece));
},
);
} else {
return dragTarget;
}
}
}