From f45ec47285e428717602bfa794d99085c1b7be18 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 25 Aug 2023 12:28:58 +0200 Subject: [PATCH] Fix green flicker of squares on invalid move --- lib/chess/chess_board.dart | 5 +++-- lib/chess_bloc/chess_bloc.dart | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/chess/chess_board.dart b/lib/chess/chess_board.dart index d50acd1..fbe8a59 100644 --- a/lib/chess/chess_board.dart +++ b/lib/chess/chess_board.dart @@ -22,8 +22,9 @@ class ChessBoard extends StatelessWidget { final piece = boardState.position[ChessCoordinate(column, row)]; bool squareWasPartOfLastMove = false; - if (boardState.lastMove.to == ChessCoordinate(column, row) || - boardState.lastMove.from == ChessCoordinate(column, row)) { + if ((boardState.lastMove.to == ChessCoordinate(column, row) || + boardState.lastMove.from == ChessCoordinate(column, row)) && + boardState.positionAckd) { squareWasPartOfLastMove = true; } diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 7e8d532..db513fc 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -43,6 +43,7 @@ class ChessBloc extends Bloc { ChessColor.white, ChessPositionManager.getInstance().currentPosition, ChessMove.none(), + false, ), ); } @@ -56,6 +57,7 @@ class ChessBloc extends Bloc { state.newTurnColor, state.position, ChessMove.none(), + false, ), ); } @@ -76,6 +78,7 @@ class ChessBloc extends Bloc { turnColor, event.position, ChessMove(from: event.startSquare, to: event.endSquare), + true, ), ); } @@ -101,12 +104,7 @@ class ChessBloc extends Bloc { tempPosition[move.from] = const ChessPiece.none(); emit( - ChessBoardState( - state.bottomColor, - turnColor, - tempPosition, - move - ), + ChessBoardState(state.bottomColor, turnColor, tempPosition, move, false), ); } @@ -131,7 +129,7 @@ class ChessBloc extends Bloc { InvalidMovePlayed event, Emitter emit) { emit( ChessBoardState(state.bottomColor, turnColor, - ChessPositionManager.getInstance().currentPosition, ChessMove.none()), + ChessPositionManager.getInstance().currentPosition, ChessMove.none(), false), ); } } @@ -141,17 +139,19 @@ class ChessBoardState { final ChessColor newTurnColor; final ChessPosition position; final ChessMove lastMove; + final bool positionAckd; ChessBoardState._( - this.bottomColor, this.newTurnColor, this.position, this.lastMove); + this.bottomColor, this.newTurnColor, this.position, this.lastMove, this.positionAckd); factory ChessBoardState( ChessColor bottomColor, ChessColor turnColor, ChessPosition position, ChessMove lastMove, + bool positionAckd, ) { - return ChessBoardState._(bottomColor, turnColor, position, lastMove); + return ChessBoardState._(bottomColor, turnColor, position, lastMove, positionAckd); } factory ChessBoardState.init() { @@ -165,6 +165,7 @@ class ChessBoardState { turnColor, ChessPositionManager.getInstance().currentPosition, ChessMove.none(), + false, ); }