More changes. Memory error when accessing state.position in ChessBloc.

This commit is contained in:
Marco 2022-11-13 02:42:10 +01:00
parent 94b7c227c9
commit b3a7418c5f
4 changed files with 83 additions and 29 deletions

View File

@ -1,6 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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_utils.dart';
import 'package:mchess/chessapp/chess_square.dart'; import 'package:mchess/chessapp/chess_square.dart';
@ -10,6 +12,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
ChessBloc._internal() : super(ChessBoardState.init()) { ChessBloc._internal() : super(ChessBoardState.init()) {
on<PieceMoved>(moveHandler); on<PieceMoved>(moveHandler);
on<PreCheckMove>(preCheckHandler);
} }
factory ChessBloc.getInstance() { factory ChessBloc.getInstance() {
@ -24,17 +27,42 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
PieceMoved event, PieceMoved event,
Emitter<ChessBoardState> emit, Emitter<ChessBoardState> emit,
) { ) {
Map<ChessCoordinate, ChessPiece> 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( emit(ChessBoardState(
!state.flipped, state.flipped,
ChessColor.black, ChessColor.black,
state.position, newPosition,
)); ));
} }
FutureOr<bool> preCheckHandler(
PreCheckMove event,
Emitter<ChessBoardState> emit,
) {
return false;
}
} }
abstract class ChessEvent {} 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 {} class BoardFlippedEvent extends ChessEvent {}
@ -43,10 +71,28 @@ class ChessBoardState {
final ChessColor turnColor; final ChessColor turnColor;
final Map<ChessCoordinate, ChessPiece> position; final Map<ChessCoordinate, ChessPiece> position;
ChessBoardState(this.flipped, this.turnColor, this.position); ChessBoardState._(this.flipped, this.turnColor, this.position);
ChessBoardState.init() factory ChessBoardState(
: flipped = false, bool flipped,
turnColor = ChessColor.white, ChessColor turnColor,
position = {}; Map<ChessCoordinate, ChessPiece> position,
) {
return ChessBoardState._(flipped, turnColor, position);
}
factory ChessBoardState.init() {
bool flipped = false;
ChessColor turnColor = ChessColor.white;
Map<ChessCoordinate, ChessPiece> 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);
}
} }

View File

@ -8,7 +8,7 @@ class ChessBoard extends StatelessWidget {
final ChessBoardState bState; final ChessBoardState bState;
final List<ChessSquare> squares; final List<ChessSquare> squares;
ChessBoard._({required this.bState, required this.squares}); const ChessBoard._({required this.bState, required this.squares});
factory ChessBoard({required ChessBoardState bState}) { factory ChessBoard({required ChessBoardState bState}) {
List<ChessSquare> squares = List.empty(growable: true); List<ChessSquare> squares = List.empty(growable: true);

View File

@ -3,7 +3,7 @@ import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'chess_utils.dart'; import 'chess_utils.dart';
class ChessSquare extends StatefulWidget { class ChessSquare extends StatelessWidget {
final ChessCoordinate coordinate; final ChessCoordinate coordinate;
late ChessPiece? containedPiece; late ChessPiece? containedPiece;
static const double pieceWidth = 200; static const double pieceWidth = 200;
@ -11,11 +11,6 @@ class ChessSquare extends StatefulWidget {
ChessSquare({required this.coordinate, this.containedPiece, super.key}); ChessSquare({required this.coordinate, this.containedPiece, super.key});
@override
State<StatefulWidget> createState() => _ChessSquareState();
}
class _ChessSquareState extends State<ChessSquare> {
late Color color; late Color color;
@override @override
@ -30,7 +25,7 @@ class _ChessSquareState extends State<ChessSquare> {
draggableFdbSize = 0.15 * windowHeight; draggableFdbSize = 0.15 * windowHeight;
} }
color = _getSquareColor(widget.coordinate); color = _getSquareColor(coordinate);
return DragTarget<ChessMove>( return DragTarget<ChessMove>(
builder: (context, candidateData, rejectedData) { builder: (context, candidateData, rejectedData) {
@ -40,23 +35,19 @@ class _ChessSquareState extends State<ChessSquare> {
height: ChessSquare.pieceWidth, height: ChessSquare.pieceWidth,
child: Draggable<ChessMove>( child: Draggable<ChessMove>(
/* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */ /* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */
data: ChessMove( data: ChessMove(coordinate, coordinate, containedPiece),
widget.coordinate, widget.coordinate, widget.containedPiece),
feedback: FractionalTranslation( feedback: FractionalTranslation(
translation: const Offset(-0.5, -0.75), translation: const Offset(-0.5, -0.75),
child: SizedBox( child: SizedBox(
height: draggableFdbSize, height: draggableFdbSize,
width: draggableFdbSize, width: draggableFdbSize,
child: widget.containedPiece), child: containedPiece),
), ),
childWhenDragging: Container(), childWhenDragging: Container(),
dragAnchorStrategy: pointerDragAnchorStrategy, dragAnchorStrategy: pointerDragAnchorStrategy,
child: widget.containedPiece ?? Container(), child: containedPiece ?? Container(),
onDragCompleted: () { onDragCompleted: () {},
setState(() { onDragStarted: () {},
widget.containedPiece = null;
});
},
), ),
); );
}, },
@ -65,7 +56,11 @@ class _ChessSquareState extends State<ChessSquare> {
return true; return true;
}, },
onAccept: (move) { onAccept: (move) {
ChessBloc().add(PieceMoved()); move.endSquare = coordinate;
ChessBloc().add(PieceMoved(
startSquare: move.startSquare,
endSquare: move.endSquare,
));
}, },
); );
} }

View File

@ -32,6 +32,7 @@ Map<ChessPieceName, String> chessPiecesAssets = {
ChessPieceName.blackRook: 'assets/pieces/black/rook.svg', ChessPieceName.blackRook: 'assets/pieces/black/rook.svg',
ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg', ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg',
ChessPieceName.blackKing: 'assets/pieces/black/king.svg', ChessPieceName.blackKing: 'assets/pieces/black/king.svg',
ChessPieceName.none: '',
}; };
class ChessCoordinate { class ChessCoordinate {
@ -39,6 +40,9 @@ class ChessCoordinate {
late int row; late int row;
ChessCoordinate(this.column, this.row); ChessCoordinate(this.column, this.row);
ChessCoordinate.copyFrom(ChessCoordinate original)
: column = original.column,
row = original.row;
static String columnIntToColumnString(int col) { static String columnIntToColumnString(int col) {
String colStr; String colStr;
@ -58,14 +62,23 @@ class ChessCoordinate {
class ChessPiece extends StatelessWidget { class ChessPiece extends StatelessWidget {
final ChessColor color; final ChessColor color;
final ChessPieceName pieceName; final ChessPieceName pieceName;
late final Widget pieceImage; final Widget? pieceImage;
ChessPiece(this.pieceName, this.color, {super.key}) { const ChessPiece._(this.pieceName, this.color, this.pieceImage);
String pieceAssetUrl = chessPiecesAssets[pieceName]!;
factory ChessPiece(ChessPieceName name, ChessColor color) {
Widget? pieceImage;
String pieceAssetUrl = chessPiecesAssets[name]!;
pieceImage = SvgPicture.asset(pieceAssetUrl); pieceImage = SvgPicture.asset(pieceAssetUrl);
return ChessPiece._(name, color, pieceImage);
} }
const ChessPiece.none({super.key})
: pieceName = ChessPieceName.none,
color = ChessColor.white,
pieceImage = null;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SizedBox( return SizedBox(