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.
This commit is contained in:
Marco 2024-02-01 11:37:00 +01:00
parent e22dc213ac
commit 200393ac76
5 changed files with 14 additions and 10 deletions

View File

@ -1,5 +1,6 @@
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';
@ -66,7 +67,8 @@ class _ChessSquareState extends State<ChessSquare> {
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
);
if (widget.containedPiece == null) {
if (widget.containedPiece == null ||
widget.containedPiece!.color != ChessBloc.getMyColor()) {
return GestureDetector(
child: dragTarget,
onTap: () {

View File

@ -30,13 +30,13 @@ class ChessSquareOuterDragTarget extends StatelessWidget {
if (isPromotionMove(
details.data.movedPiece!.pieceClass,
ChessBloc.myColor!,
ChessBloc.getMyColor(),
details.data.toSquare,
)) {
var move = ChessMove(
from: details.data.fromSquare, to: details.data.toSquare);
PromotionBloc().add(PawnMovedToPromotionField(
move: move, colorMoved: ChessBloc.myColor!));
move: move, colorMoved: ChessBloc.myColor));
} else if (coordinate != details.data.fromSquare) {
ChessBloc().add(OwnPieceMoved(
startSquare: details.data.fromSquare,

View File

@ -11,9 +11,9 @@ import 'package:mchess/utils/chess_utils.dart';
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal();
static ChessColor turnColor = ChessColor.white;
static ChessColor? myColor = ChessColor.white;
static ChessColor myColor = ChessColor.white;
static ChessColor? getSidesColor() {
static ChessColor getMyColor() {
return myColor;
}
@ -126,7 +126,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
OwnPromotionPlayed event, Emitter<ChessBoardState> emit) {
var apiMove = event.move.toApiMove();
var shorNameForPiece = chessPiecesShortName[
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]!;
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor)]!;
apiMove.promotionToPiece = shorNameForPiece;
var message = ApiWebsocketMessage(
type: MessageType.move,

View File

@ -51,12 +51,12 @@ class TapBloc extends Bloc<TapEvent, TapState> {
state.firstSquareTapped != event.tapped) {
if (isPromotionMove(
state.pieceOnFirstTappedSquare!.pieceClass,
ChessBloc.myColor!,
ChessBloc.myColor,
event.tapped,
)) {
PromotionBloc().add(PawnMovedToPromotionField(
move: ChessMove(from: state.firstSquareTapped!, to: event.tapped),
colorMoved: ChessBloc.myColor!));
colorMoved: ChessBloc.myColor));
emit(TapState.init());
return;
} else {

View File

@ -168,8 +168,10 @@ class _LobbySelectorState extends State<LobbySelector> {
backgroundColor: Colors.amberAccent,
content: Text("mChess server is not responding. Try again or give up"),
);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
if (mounted) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
return null;
}