Fix late/final variables. Fix calls to ChessBloc by using context.

This commit is contained in:
Marco 2022-11-13 13:09:36 +01:00
parent 0ab37c5564
commit a6880dd0e9
2 changed files with 46 additions and 38 deletions

View File

@ -20,8 +20,8 @@ class ChessBoard extends StatelessWidget {
squares.add(
ChessSquare(
coordinate: ChessCoordinate(column, row),
containedPiece: piece,
ChessCoordinate(column, row),
piece,
),
);
}

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
@ -7,13 +8,44 @@ import 'chess_utils.dart';
class ChessSquare extends StatelessWidget {
final ChessCoordinate coordinate;
late ChessPiece? containedPiece;
final ChessPiece? containedPiece;
static const double pieceWidth = 200;
static const double pieceHeight = 200;
ChessSquare({required this.coordinate, this.containedPiece, super.key});
final Color color;
late Color color;
const ChessSquare._(
{required this.coordinate,
required this.containedPiece,
required this.color,
super.key});
factory ChessSquare(ChessCoordinate coord, ChessPiece? piece) {
Color lightSquares = Colors.brown.shade50;
Color darkSquares = Colors.brown.shade400;
Color squareColor;
if (coord.row % 2 == 0) {
if (coord.column % 2 == 0) {
squareColor = darkSquares;
} else {
squareColor = lightSquares;
}
} else {
if (coord.column % 2 == 0) {
squareColor = lightSquares;
} else {
squareColor = darkSquares;
}
}
return ChessSquare._(
coordinate: coord,
containedPiece: piece,
color: squareColor,
);
}
@override
Widget build(BuildContext context) {
@ -27,8 +59,6 @@ class ChessSquare extends StatelessWidget {
draggableFdbSize = 0.15 * windowHeight;
}
color = _getSquareColor(coordinate);
return DragTarget<ChessMove>(
builder: (context, candidateData, rejectedData) {
return Container(
@ -54,8 +84,9 @@ class ChessSquare extends StatelessWidget {
);
},
onWillAccept: (move) {
final legalMove =
ChessBloc().preCheckHandler(PreCheckMove(move: move!));
final legalMove = context
.read<ChessBloc>()
.preCheckHandler(PreCheckMove(move: move!));
print('onWillAccept returns $legalMove');
return legalMove;
},
@ -63,37 +94,14 @@ class ChessSquare extends StatelessWidget {
move.endSquare = coordinate;
if (move.endSquare != move.startSquare) {
ChessBloc().add(
PieceMoved(
startSquare: move.startSquare,
endSquare: move.endSquare,
),
);
context.read<ChessBloc>().add(
PieceMoved(
startSquare: move.startSquare,
endSquare: move.endSquare,
),
);
}
},
);
}
Color _getSquareColor(ChessCoordinate coordinate) {
Color lightSquares = Colors.brown.shade50;
Color darkSquares = Colors.brown.shade400;
Color squareColor;
if (coordinate.row % 2 == 0) {
if (coordinate.column % 2 == 0) {
squareColor = darkSquares;
} else {
squareColor = lightSquares;
}
} else {
if (coordinate.column % 2 == 0) {
squareColor = lightSquares;
} else {
squareColor = darkSquares;
}
}
return squareColor;
}
}