More changes. Memory error when accessing state.position in ChessBloc.
This commit is contained in:
parent
94b7c227c9
commit
b3a7418c5f
@ -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<ChessEvent, ChessBoardState> {
|
||||
|
||||
ChessBloc._internal() : super(ChessBoardState.init()) {
|
||||
on<PieceMoved>(moveHandler);
|
||||
on<PreCheckMove>(preCheckHandler);
|
||||
}
|
||||
|
||||
factory ChessBloc.getInstance() {
|
||||
@ -24,17 +27,42 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
||||
PieceMoved event,
|
||||
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(
|
||||
!state.flipped,
|
||||
state.flipped,
|
||||
ChessColor.black,
|
||||
state.position,
|
||||
newPosition,
|
||||
));
|
||||
}
|
||||
|
||||
FutureOr<bool> preCheckHandler(
|
||||
PreCheckMove event,
|
||||
Emitter<ChessBoardState> 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<ChessCoordinate, ChessPiece> 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<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);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ class ChessBoard extends StatelessWidget {
|
||||
final ChessBoardState bState;
|
||||
final List<ChessSquare> squares;
|
||||
|
||||
ChessBoard._({required this.bState, required this.squares});
|
||||
const ChessBoard._({required this.bState, required this.squares});
|
||||
|
||||
factory ChessBoard({required ChessBoardState bState}) {
|
||||
List<ChessSquare> squares = List.empty(growable: true);
|
||||
|
@ -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<StatefulWidget> createState() => _ChessSquareState();
|
||||
}
|
||||
|
||||
class _ChessSquareState extends State<ChessSquare> {
|
||||
late Color color;
|
||||
|
||||
@override
|
||||
@ -30,7 +25,7 @@ class _ChessSquareState extends State<ChessSquare> {
|
||||
draggableFdbSize = 0.15 * windowHeight;
|
||||
}
|
||||
|
||||
color = _getSquareColor(widget.coordinate);
|
||||
color = _getSquareColor(coordinate);
|
||||
|
||||
return DragTarget<ChessMove>(
|
||||
builder: (context, candidateData, rejectedData) {
|
||||
@ -40,23 +35,19 @@ class _ChessSquareState extends State<ChessSquare> {
|
||||
height: ChessSquare.pieceWidth,
|
||||
child: Draggable<ChessMove>(
|
||||
/* 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<ChessSquare> {
|
||||
return true;
|
||||
},
|
||||
onAccept: (move) {
|
||||
ChessBloc().add(PieceMoved());
|
||||
move.endSquare = coordinate;
|
||||
ChessBloc().add(PieceMoved(
|
||||
startSquare: move.startSquare,
|
||||
endSquare: move.endSquare,
|
||||
));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ Map<ChessPieceName, String> 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(
|
||||
|
Loading…
Reference in New Issue
Block a user