From 130a2dae79808ef8e6a7b728994db2e5dda66e4c Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 4 Sep 2023 21:52:40 +0200 Subject: [PATCH] Make ChessSquare a stateful widget. --- lib/chess/chess_square.dart | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/chess/chess_square.dart b/lib/chess/chess_square.dart index 8f32b3c..256be0c 100644 --- a/lib/chess/chess_square.dart +++ b/lib/chess/chess_square.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess/chess_square_outer_dragtarget.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; import '../utils/chess_utils.dart'; -class ChessSquare extends StatelessWidget { +class ChessSquare extends StatefulWidget { final ChessCoordinate coordinate; final ChessPiece? containedPiece; static const double pieceWidth = 200; @@ -46,13 +48,35 @@ class ChessSquare extends StatelessWidget { ); } + @override + State createState() => _ChessSquareState(); +} + +class _ChessSquareState extends State { + late Color squareColor; + + @override + void initState() { + squareColor = widget.color; + super.initState(); + } + @override Widget build(BuildContext context) { - return Container( - color: color, - child: ChessSquareOuterDragTarget( - coordinate: coordinate, - containedPiece: containedPiece ?? const ChessPiece.none()), + return BlocListener( + listenWhen: (previous, current) { + return true; + }, + listener: (context, state) { + setState(() { + squareColor = Colors.red; + });}, + child: Container( + color: widget.color, + child: ChessSquareOuterDragTarget( + coordinate: widget.coordinate, + containedPiece: widget.containedPiece ?? const ChessPiece.none()), + ), ); } }