Make ChessSquare a stateful widget.
This commit is contained in:
parent
01dcc74cfc
commit
130a2dae79
@ -1,8 +1,10 @@
|
|||||||
import 'package:flutter/material.dart';
|
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/chess_square_outer_dragtarget.dart';
|
||||||
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
||||||
import '../utils/chess_utils.dart';
|
import '../utils/chess_utils.dart';
|
||||||
|
|
||||||
class ChessSquare extends StatelessWidget {
|
class ChessSquare extends StatefulWidget {
|
||||||
final ChessCoordinate coordinate;
|
final ChessCoordinate coordinate;
|
||||||
final ChessPiece? containedPiece;
|
final ChessPiece? containedPiece;
|
||||||
static const double pieceWidth = 200;
|
static const double pieceWidth = 200;
|
||||||
@ -46,13 +48,35 @@ class ChessSquare extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<ChessSquare> createState() => _ChessSquareState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ChessSquareState extends State<ChessSquare> {
|
||||||
|
late Color squareColor;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
squareColor = widget.color;
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Container(
|
return BlocListener<ChessBloc, ChessBoardState>(
|
||||||
color: color,
|
listenWhen: (previous, current) {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
listener: (context, state) {
|
||||||
|
setState(() {
|
||||||
|
squareColor = Colors.red;
|
||||||
|
});},
|
||||||
|
child: Container(
|
||||||
|
color: widget.color,
|
||||||
child: ChessSquareOuterDragTarget(
|
child: ChessSquareOuterDragTarget(
|
||||||
coordinate: coordinate,
|
coordinate: widget.coordinate,
|
||||||
containedPiece: containedPiece ?? const ChessPiece.none()),
|
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user