Skip to content

Commit

Permalink
use gesture detector instead of onPointerDown to fire tap callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-anders committed Jan 6, 2025
1 parent 5c95a99 commit d12490a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
97 changes: 52 additions & 45 deletions lib/src/widgets/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,50 +375,59 @@ class _BoardState extends State<Chessboard> {

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);
},
),
],
],
),
),
),
);
Expand Down Expand Up @@ -570,8 +579,6 @@ class _BoardState extends State<Chessboard> {
final square = widget.offsetSquare(details.localPosition);
if (square == null) return;

widget.onTappedSquare?.call(square);

final Piece? piece = pieces[square];

if (widget.settings.drawShape.enable) {
Expand Down
5 changes: 1 addition & 4 deletions test/widgets/board_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down

0 comments on commit d12490a

Please sign in to comment.