Start a tap with drag

Now, only the empty squares contain a GestureDetector.
The GestureDetector is not necessary anymore in squares with pieces
because the tap is started when the drag is started.
This commit is contained in:
Marco 2024-02-01 11:11:11 +01:00
parent c9381aaa4c
commit 6882505174
6 changed files with 92 additions and 40 deletions

1
devtools_options.yaml Normal file
View File

@ -0,0 +1 @@
extensions:

View File

@ -59,17 +59,23 @@ class ChessSquare extends StatefulWidget {
class _ChessSquareState extends State<ChessSquare> {
@override
Widget build(BuildContext context) {
return GestureDetector(
child: Container(
var dragTarget = Container(
color: widget.color,
child: ChessSquareOuterDragTarget(
coordinate: widget.coordinate,
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
),
);
if (widget.containedPiece == null) {
return GestureDetector(
child: dragTarget,
onTap: () {
TapBloc().add(SquareTappedEvent(
tapped: widget.coordinate, pieceOnSquare: widget.containedPiece));
},
);
} else {
return dragTarget;
}
}
}

View File

@ -50,7 +50,8 @@ class ChessSquareInnerDraggable extends StatelessWidget {
child: containedPiece ?? Container(),
onDragCompleted: () {},
onDragStarted: () {
TapBloc().add(CancelTapEvent());
TapBloc().add(SquareTappedEvent(
tapped: coordinate, pieceOnSquare: containedPiece));
},
),
);

View File

@ -3,6 +3,7 @@ import 'package:mchess/chess/chess_square_inner_draggable.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/chess_bloc/promotion_bloc.dart';
import 'package:mchess/chess_bloc/tap_bloc.dart';
import 'package:mchess/utils/chess_utils.dart';
class ChessSquareOuterDragTarget extends StatelessWidget {
@ -15,29 +16,31 @@ class ChessSquareOuterDragTarget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DragTarget<PieceDragged>(
onWillAccept: (move) {
if (move?.fromSquare == coordinate) {
onWillAcceptWithDetails: (details) {
if (details.data.fromSquare == coordinate) {
return false;
}
return true;
},
onAccept: (pieceDragged) {
onAcceptWithDetails: (details) {
// Replace the dummy value with the actual target of the move.
pieceDragged.toSquare = coordinate;
details.data.toSquare = coordinate;
TapBloc().add(CancelTapEvent());
if (isPromotionMove(
pieceDragged.movedPiece!.pieceClass,
details.data.movedPiece!.pieceClass,
ChessBloc.myColor!,
pieceDragged.toSquare,
details.data.toSquare,
)) {
var move = ChessMove(
from: pieceDragged.fromSquare, to: pieceDragged.toSquare);
from: details.data.fromSquare, to: details.data.toSquare);
PromotionBloc().add(PawnMovedToPromotionField(
move: move, colorMoved: ChessBloc.myColor!));
} else if (coordinate != pieceDragged.fromSquare) {
} else if (coordinate != details.data.fromSquare) {
ChessBloc().add(OwnPieceMoved(
startSquare: pieceDragged.fromSquare,
endSquare: pieceDragged.toSquare));
startSquare: details.data.fromSquare,
endSquare: details.data.toSquare));
}
},
builder: (context, candidateData, rejectedData) {

View File

@ -57,6 +57,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.18.0"
conditional_wrap:
dependency: "direct main"
description:
name: conditional_wrap
sha256: "3428b891b37c5e690baa362e0fd4cad9b346b533f1f8f7aeff13cd2bebd86a5b"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
crypto:
dependency: transitive
description:
@ -85,10 +93,10 @@ packages:
dependency: transitive
description:
name: ffi
sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878"
sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.1.2"
file:
dependency: transitive
description:
@ -148,10 +156,10 @@ packages:
dependency: "direct main"
description:
name: go_router
sha256: "3b40e751eaaa855179b416974d59d29669e750d2e50fcdb2b37f1cb0ca8c803a"
sha256: "07ee2436909f749d606f53521dc1725dd738dc5196e5ff815bc254253c594075"
url: "https://pub.dev"
source: hosted
version: "13.0.1"
version: "13.1.0"
http:
dependency: "direct main"
description:
@ -168,6 +176,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
lints:
dependency: transitive
description:
@ -188,26 +220,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
nested:
dependency: transitive
description:
@ -220,10 +252,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
path_parsing:
dependency: transitive
description:
@ -469,22 +501,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "13.0.0"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
sha256: "4188706108906f002b3a293509234588823c8c979dc83304e229ff400c996b05"
url: "https://pub.dev"
source: hosted
version: "0.3.0"
version: "0.4.2"
web_socket_channel:
dependency: "direct main"
description:
name: web_socket_channel
sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b
sha256: "939ab60734a4f8fa95feacb55804fa278de28bdeef38e616dc08e44a84adea23"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
version: "2.4.3"
win32:
dependency: transitive
description:
@ -510,5 +550,5 @@ packages:
source: hosted
version: "6.5.0"
sdks:
dart: ">=3.2.0 <4.0.0"
dart: ">=3.3.0-279.1.beta <4.0.0"
flutter: ">=3.16.0"

View File

@ -40,12 +40,13 @@ dependencies:
cupertino_icons: ^1.0.2
flutter_bloc: ^8.1.3
quiver: ^3.1.0
web_socket_channel: ^2.4.0
web_socket_channel: ^2.4.3
go_router: ^13.0.0
http: ^1.0.0
uuid: ^4.0.0
shared_preferences: ^2.2.2
universal_platform: ^1.0.0+1
conditional_wrap: ^3.0.0
dev_dependencies:
flutter_test: