mchess-client/lib/chess/chess_square_inner_draggable.dart

60 lines
2.0 KiB
Dart
Raw Permalink Normal View History

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';
import 'package:mchess/chess_bloc/tap_bloc.dart';
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: () {},
onDragStarted: () {
TapBloc().add(SquareTappedEvent(
tapped: coordinate, pieceOnSquare: containedPiece));
},
),
);
}
}