2023-09-04 19:39:51 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:mchess/chess/chess_square.dart';
|
|
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
2024-01-09 11:18:10 +00:00
|
|
|
import 'package:mchess/chess_bloc/tap_bloc.dart';
|
2023-09-04 19:39:51 +00:00
|
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
|
|
|
|
|
|
class ChessSquareInnerDraggable extends StatelessWidget {
|
|
|
|
const ChessSquareInnerDraggable({
|
|
|
|
super.key,
|
|
|
|
required this.coordinate,
|
|
|
|
required this.containedPiece,
|
|
|
|
});
|
|
|
|
|
|
|
|
final ChessCoordinate coordinate;
|
|
|
|
final ChessPiece? containedPiece;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
double windowWidth = MediaQuery.of(context).size.width;
|
|
|
|
double windowHeight = MediaQuery.of(context).size.height;
|
|
|
|
double draggableFdbSize;
|
|
|
|
|
|
|
|
var canMove = (ChessBloc.turnColor == ChessBloc.myColor) &&
|
|
|
|
(containedPiece?.color == ChessBloc.turnColor);
|
|
|
|
var maxDrags = kDebugMode ? 1 : (canMove ? 1 : 0);
|
|
|
|
|
|
|
|
if (windowWidth < windowHeight) {
|
|
|
|
draggableFdbSize = 0.15 * windowWidth;
|
|
|
|
} else {
|
|
|
|
draggableFdbSize = 0.15 * windowHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SizedBox(
|
|
|
|
width: ChessSquare.pieceWidth,
|
|
|
|
height: ChessSquare.pieceWidth,
|
|
|
|
child: Draggable<PieceDragged>(
|
|
|
|
/* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */
|
|
|
|
data: PieceDragged(coordinate, coordinate, containedPiece),
|
|
|
|
maxSimultaneousDrags: maxDrags,
|
|
|
|
feedback: FractionalTranslation(
|
|
|
|
translation: const Offset(-0.5, -0.75),
|
|
|
|
child: SizedBox(
|
|
|
|
height: draggableFdbSize,
|
|
|
|
width: draggableFdbSize,
|
|
|
|
child: containedPiece),
|
|
|
|
),
|
|
|
|
childWhenDragging: Container(),
|
|
|
|
dragAnchorStrategy: pointerDragAnchorStrategy,
|
|
|
|
child: containedPiece ?? Container(),
|
|
|
|
onDragCompleted: () {},
|
2024-01-09 11:18:10 +00:00
|
|
|
onDragStarted: () {
|
|
|
|
TapBloc().add(CancelTapEvent());
|
|
|
|
},
|
2023-09-04 19:39:51 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|