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( squares.add(
ChessSquare( ChessSquare(
coordinate: ChessCoordinate(column, row), ChessCoordinate(column, row),
containedPiece: piece, piece,
), ),
); );
} }

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; 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_events.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart';
@ -7,13 +8,44 @@ import 'chess_utils.dart';
class ChessSquare extends StatelessWidget { class ChessSquare extends StatelessWidget {
final ChessCoordinate coordinate; final ChessCoordinate coordinate;
late ChessPiece? containedPiece; final ChessPiece? containedPiece;
static const double pieceWidth = 200; static const double pieceWidth = 200;
static const double pieceHeight = 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -27,8 +59,6 @@ class ChessSquare extends StatelessWidget {
draggableFdbSize = 0.15 * windowHeight; draggableFdbSize = 0.15 * windowHeight;
} }
color = _getSquareColor(coordinate);
return DragTarget<ChessMove>( return DragTarget<ChessMove>(
builder: (context, candidateData, rejectedData) { builder: (context, candidateData, rejectedData) {
return Container( return Container(
@ -54,8 +84,9 @@ class ChessSquare extends StatelessWidget {
); );
}, },
onWillAccept: (move) { onWillAccept: (move) {
final legalMove = final legalMove = context
ChessBloc().preCheckHandler(PreCheckMove(move: move!)); .read<ChessBloc>()
.preCheckHandler(PreCheckMove(move: move!));
print('onWillAccept returns $legalMove'); print('onWillAccept returns $legalMove');
return legalMove; return legalMove;
}, },
@ -63,7 +94,7 @@ class ChessSquare extends StatelessWidget {
move.endSquare = coordinate; move.endSquare = coordinate;
if (move.endSquare != move.startSquare) { if (move.endSquare != move.startSquare) {
ChessBloc().add( context.read<ChessBloc>().add(
PieceMoved( PieceMoved(
startSquare: move.startSquare, startSquare: move.startSquare,
endSquare: move.endSquare, endSquare: move.endSquare,
@ -73,27 +104,4 @@ class ChessSquare extends StatelessWidget {
}, },
); );
} }
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;
}
} }