From b3a7418c5fa024e6edf99bd64e9cc72e507cbd7d Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 13 Nov 2022 02:42:10 +0100 Subject: [PATCH] More changes. Memory error when accessing state.position in ChessBloc. --- lib/chess_bloc/chess_bloc.dart | 62 +++++++++++++++++++++++++++++----- lib/chessapp/chess_board.dart | 2 +- lib/chessapp/chess_square.dart | 29 +++++++--------- lib/chessapp/chess_utils.dart | 19 +++++++++-- 4 files changed, 83 insertions(+), 29 deletions(-) diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 9a1848d..aeb88e5 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -1,6 +1,8 @@ import 'dart:async'; +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:mchess/chessapp/chess_board.dart'; import 'package:mchess/chessapp/chess_utils.dart'; import 'package:mchess/chessapp/chess_square.dart'; @@ -10,6 +12,7 @@ class ChessBloc extends Bloc { ChessBloc._internal() : super(ChessBoardState.init()) { on(moveHandler); + on(preCheckHandler); } factory ChessBloc.getInstance() { @@ -24,17 +27,42 @@ class ChessBloc extends Bloc { PieceMoved event, Emitter emit, ) { + Map newPosition = {}; + + newPosition[ChessCoordinate(5, 5)] = + ChessPiece(ChessPieceName.blackBishop, ChessColor.black); + + ChessPiece? piece = + state.position[ChessCoordinate.copyFrom(event.startSquare)]; + + newPosition[event.endSquare] = piece!; + newPosition[event.startSquare] = const ChessPiece.none(); + emit(ChessBoardState( - !state.flipped, + state.flipped, ChessColor.black, - state.position, + newPosition, )); } + + FutureOr preCheckHandler( + PreCheckMove event, + Emitter emit, + ) { + return false; + } } abstract class ChessEvent {} -class PieceMoved extends ChessEvent {} +class PieceMoved extends ChessEvent { + final ChessCoordinate startSquare; + final ChessCoordinate endSquare; + + PieceMoved({required this.startSquare, required this.endSquare}); +} + +class PreCheckMove extends ChessEvent {} class BoardFlippedEvent extends ChessEvent {} @@ -43,10 +71,28 @@ class ChessBoardState { final ChessColor turnColor; final Map position; - ChessBoardState(this.flipped, this.turnColor, this.position); + ChessBoardState._(this.flipped, this.turnColor, this.position); - ChessBoardState.init() - : flipped = false, - turnColor = ChessColor.white, - position = {}; + factory ChessBoardState( + bool flipped, + ChessColor turnColor, + Map position, + ) { + return ChessBoardState._(flipped, turnColor, position); + } + + factory ChessBoardState.init() { + bool flipped = false; + ChessColor turnColor = ChessColor.white; + Map position = {}; + + for (int row = 1; row <= 8; row++) { + for (int col = 1; col <= 8; col++) { + position[ChessCoordinate(row, col)] = + ChessPiece(ChessPieceName.none, ChessColor.white); + } + } + + return ChessBoardState._(flipped, turnColor, position); + } } diff --git a/lib/chessapp/chess_board.dart b/lib/chessapp/chess_board.dart index e8de47f..4732d29 100644 --- a/lib/chessapp/chess_board.dart +++ b/lib/chessapp/chess_board.dart @@ -8,7 +8,7 @@ class ChessBoard extends StatelessWidget { final ChessBoardState bState; final List squares; - ChessBoard._({required this.bState, required this.squares}); + const ChessBoard._({required this.bState, required this.squares}); factory ChessBoard({required ChessBoardState bState}) { List squares = List.empty(growable: true); diff --git a/lib/chessapp/chess_square.dart b/lib/chessapp/chess_square.dart index fb3fbc7..59d4bca 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chessapp/chess_square.dart @@ -3,7 +3,7 @@ import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'chess_utils.dart'; -class ChessSquare extends StatefulWidget { +class ChessSquare extends StatelessWidget { final ChessCoordinate coordinate; late ChessPiece? containedPiece; static const double pieceWidth = 200; @@ -11,11 +11,6 @@ class ChessSquare extends StatefulWidget { ChessSquare({required this.coordinate, this.containedPiece, super.key}); - @override - State createState() => _ChessSquareState(); -} - -class _ChessSquareState extends State { late Color color; @override @@ -30,7 +25,7 @@ class _ChessSquareState extends State { draggableFdbSize = 0.15 * windowHeight; } - color = _getSquareColor(widget.coordinate); + color = _getSquareColor(coordinate); return DragTarget( builder: (context, candidateData, rejectedData) { @@ -40,23 +35,19 @@ class _ChessSquareState extends State { height: ChessSquare.pieceWidth, child: Draggable( /* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */ - data: ChessMove( - widget.coordinate, widget.coordinate, widget.containedPiece), + data: ChessMove(coordinate, coordinate, containedPiece), feedback: FractionalTranslation( translation: const Offset(-0.5, -0.75), child: SizedBox( height: draggableFdbSize, width: draggableFdbSize, - child: widget.containedPiece), + child: containedPiece), ), childWhenDragging: Container(), dragAnchorStrategy: pointerDragAnchorStrategy, - child: widget.containedPiece ?? Container(), - onDragCompleted: () { - setState(() { - widget.containedPiece = null; - }); - }, + child: containedPiece ?? Container(), + onDragCompleted: () {}, + onDragStarted: () {}, ), ); }, @@ -65,7 +56,11 @@ class _ChessSquareState extends State { return true; }, onAccept: (move) { - ChessBloc().add(PieceMoved()); + move.endSquare = coordinate; + ChessBloc().add(PieceMoved( + startSquare: move.startSquare, + endSquare: move.endSquare, + )); }, ); } diff --git a/lib/chessapp/chess_utils.dart b/lib/chessapp/chess_utils.dart index e71300b..45ecf51 100644 --- a/lib/chessapp/chess_utils.dart +++ b/lib/chessapp/chess_utils.dart @@ -32,6 +32,7 @@ Map chessPiecesAssets = { ChessPieceName.blackRook: 'assets/pieces/black/rook.svg', ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg', ChessPieceName.blackKing: 'assets/pieces/black/king.svg', + ChessPieceName.none: '', }; class ChessCoordinate { @@ -39,6 +40,9 @@ class ChessCoordinate { late int row; ChessCoordinate(this.column, this.row); + ChessCoordinate.copyFrom(ChessCoordinate original) + : column = original.column, + row = original.row; static String columnIntToColumnString(int col) { String colStr; @@ -58,14 +62,23 @@ class ChessCoordinate { class ChessPiece extends StatelessWidget { final ChessColor color; final ChessPieceName pieceName; - late final Widget pieceImage; + final Widget? pieceImage; - ChessPiece(this.pieceName, this.color, {super.key}) { - String pieceAssetUrl = chessPiecesAssets[pieceName]!; + const ChessPiece._(this.pieceName, this.color, this.pieceImage); + + factory ChessPiece(ChessPieceName name, ChessColor color) { + Widget? pieceImage; + String pieceAssetUrl = chessPiecesAssets[name]!; pieceImage = SvgPicture.asset(pieceAssetUrl); + return ChessPiece._(name, color, pieceImage); } + const ChessPiece.none({super.key}) + : pieceName = ChessPieceName.none, + color = ChessColor.white, + pieceImage = null; + @override Widget build(BuildContext context) { return SizedBox(