From d12490a07b6a219d65731e343e9dde0b6d1f44c2 Mon Sep 17 00:00:00 2001 From: tom-anders <13141438+tom-anders@users.noreply.github.com> Date: Mon, 6 Jan 2025 14:01:11 +0100 Subject: [PATCH] use gesture detector instead of onPointerDown to fire tap callback --- lib/src/widgets/board.dart | 97 +++++++++++++++++++----------------- test/widgets/board_test.dart | 5 +- 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/lib/src/widgets/board.dart b/lib/src/widgets/board.dart index 4510795..fb5005e 100644 --- a/lib/src/widgets/board.dart +++ b/lib/src/widgets/board.dart @@ -375,50 +375,59 @@ class _BoardState extends State { final enableListeners = widget.interactive || settings.drawShape.enable; - final board = Listener( - onPointerDown: enableListeners ? _onPointerDown : null, - onPointerMove: enableListeners ? _onPointerMove : null, - onPointerUp: enableListeners ? _onPointerUp : null, - onPointerCancel: enableListeners ? _onPointerCancel : null, - child: SizedBox.square( - key: const ValueKey('board-container'), - dimension: widget.size, - child: Stack( - alignment: Alignment.topLeft, - clipBehavior: Clip.none, - children: [ - if (settings.border == null && - (settings.boxShadow.isNotEmpty || - settings.borderRadius != BorderRadius.zero)) - Container( - key: const ValueKey('background-container'), - clipBehavior: Clip.hardEdge, - decoration: BoxDecoration( - borderRadius: settings.borderRadius, - boxShadow: settings.boxShadow, - ), - child: Stack( - alignment: Alignment.topLeft, - children: highlightedBackground, + final board = GestureDetector( + behavior: HitTestBehavior.translucent, + onTapUp: (details) { + final square = widget.offsetSquare(details.localPosition); + if (square != null) { + widget.onTappedSquare?.call(square); + } + }, + child: Listener( + onPointerDown: enableListeners ? _onPointerDown : null, + onPointerMove: enableListeners ? _onPointerMove : null, + onPointerUp: enableListeners ? _onPointerUp : null, + onPointerCancel: enableListeners ? _onPointerCancel : null, + child: SizedBox.square( + key: const ValueKey('board-container'), + dimension: widget.size, + child: Stack( + alignment: Alignment.topLeft, + clipBehavior: Clip.none, + children: [ + if (settings.border == null && + (settings.boxShadow.isNotEmpty || + settings.borderRadius != BorderRadius.zero)) + Container( + key: const ValueKey('background-container'), + clipBehavior: Clip.hardEdge, + decoration: BoxDecoration( + borderRadius: settings.borderRadius, + boxShadow: settings.boxShadow, + ), + child: Stack( + alignment: Alignment.topLeft, + children: highlightedBackground, + ), + ) + else + ...highlightedBackground, + ...objects, + if (widget.game?.promotionMove != null) + PromotionSelector( + pieceAssets: settings.pieceAssets, + move: widget.game!.promotionMove!, + size: widget.size, + color: widget.game!.sideToMove, + orientation: widget.orientation, + piecesUpsideDown: _isUpsideDown(widget.game!.sideToMove), + onSelect: widget.game!.onPromotionSelection, + onCancel: () { + widget.game!.onPromotionSelection(null); + }, ), - ) - else - ...highlightedBackground, - ...objects, - if (widget.game?.promotionMove != null) - PromotionSelector( - pieceAssets: settings.pieceAssets, - move: widget.game!.promotionMove!, - size: widget.size, - color: widget.game!.sideToMove, - orientation: widget.orientation, - piecesUpsideDown: _isUpsideDown(widget.game!.sideToMove), - onSelect: widget.game!.onPromotionSelection, - onCancel: () { - widget.game!.onPromotionSelection(null); - }, - ), - ], + ], + ), ), ), ); @@ -570,8 +579,6 @@ class _BoardState extends State { final square = widget.offsetSquare(details.localPosition); if (square == null) return; - widget.onTappedSquare?.call(square); - final Piece? piece = pieces[square]; if (widget.settings.drawShape.enable) { diff --git a/test/widgets/board_test.dart b/test/widgets/board_test.dart index b7b70b1..e490631 100644 --- a/test/widgets/board_test.dart +++ b/test/widgets/board_test.dart @@ -259,10 +259,7 @@ void main() { expect(find.byKey(const Key('e2-lastMove')), findsOneWidget); expect(find.byKey(const Key('e4-lastMove')), findsOneWidget); - verifyInOrder([ - () => onTappedSquare.call(Square.e2), - () => onTappedSquare.call(Square.e2), - ]); + verify(() => onTappedSquare.call(Square.e2)).called(1); verifyNoMoreInteractions(onTappedSquare); } });