From a6880dd0e9e2b9c73677737e2f000dcd3eac5430 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 13 Nov 2022 13:09:36 +0100 Subject: [PATCH] Fix late/final variables. Fix calls to ChessBloc by using context. --- lib/chessapp/chess_board.dart | 4 +- lib/chessapp/chess_square.dart | 80 +++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/lib/chessapp/chess_board.dart b/lib/chessapp/chess_board.dart index f938aff..6ab2282 100644 --- a/lib/chessapp/chess_board.dart +++ b/lib/chessapp/chess_board.dart @@ -20,8 +20,8 @@ class ChessBoard extends StatelessWidget { squares.add( ChessSquare( - coordinate: ChessCoordinate(column, row), - containedPiece: piece, + ChessCoordinate(column, row), + piece, ), ); } diff --git a/lib/chessapp/chess_square.dart b/lib/chessapp/chess_square.dart index 9865d37..7e6f70c 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chessapp/chess_square.dart @@ -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( 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() + .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().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; - } }