mchess-client/lib/chess/chess_square.dart

84 lines
2.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2023-09-04 19:52:40 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mchess/chess/chess_square_outer_dragtarget.dart';
2023-09-04 19:52:40 +00:00
import 'package:mchess/chess_bloc/chess_bloc.dart';
2022-12-25 15:16:23 +00:00
import '../utils/chess_utils.dart';
2023-09-04 19:52:40 +00:00
class ChessSquare extends StatefulWidget {
final ChessCoordinate coordinate;
final ChessPiece? containedPiece;
static const double pieceWidth = 200;
static const double pieceHeight = 200;
final Color color;
const ChessSquare._({
required this.coordinate,
required this.containedPiece,
required this.color,
});
factory ChessSquare(
ChessCoordinate coord, ChessPiece? piece, bool wasPartOfLastMove) {
Color lightSquaresColor =
wasPartOfLastMove ? Colors.green.shade200 : Colors.brown.shade50;
Color darkSquaresColor =
wasPartOfLastMove ? Colors.green.shade300 : Colors.brown.shade400;
Color squareColor;
if (coord.row % 2 == 0) {
if (coord.column % 2 == 0) {
squareColor = darkSquaresColor;
} else {
squareColor = lightSquaresColor;
}
} else {
if (coord.column % 2 == 0) {
squareColor = lightSquaresColor;
} else {
squareColor = darkSquaresColor;
}
}
return ChessSquare._(
coordinate: coord,
containedPiece: piece,
color: squareColor,
);
}
2023-09-04 19:52:40 +00:00
@override
State<ChessSquare> createState() => _ChessSquareState();
}
class _ChessSquareState extends State<ChessSquare> {
late Color squareColor;
@override
void initState() {
squareColor = widget.color;
super.initState();
}
@override
Widget build(BuildContext context) {
2023-09-04 19:52:40 +00:00
return BlocListener<ChessBloc, ChessBoardState>(
listenWhen: (previous, current) {
return true;
},
listener: (context, state) {
setState(() {
squareColor = Colors.red;
});
},
2023-09-04 19:52:40 +00:00
child: Container(
color: widget.color,
child: ChessSquareOuterDragTarget(
coordinate: widget.coordinate,
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
),
);
}
}