Cancel tap if a drag is started (tapped square will not stay red in case a drag is started)

This commit is contained in:
Marco 2024-01-09 12:18:10 +01:00
parent 68f84507e9
commit ac785922f5
2 changed files with 11 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mchess/chess/chess_square.dart'; import 'package:mchess/chess/chess_square.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/tap_bloc.dart';
import 'package:mchess/utils/chess_utils.dart'; import 'package:mchess/utils/chess_utils.dart';
class ChessSquareInnerDraggable extends StatelessWidget { class ChessSquareInnerDraggable extends StatelessWidget {
@ -48,7 +49,9 @@ class ChessSquareInnerDraggable extends StatelessWidget {
dragAnchorStrategy: pointerDragAnchorStrategy, dragAnchorStrategy: pointerDragAnchorStrategy,
child: containedPiece ?? Container(), child: containedPiece ?? Container(),
onDragCompleted: () {}, onDragCompleted: () {},
onDragStarted: () {}, onDragStarted: () {
TapBloc().add(CancelTapEvent());
},
), ),
); );
} }

View File

@ -19,6 +19,7 @@ class TapBloc extends Bloc<TapEvent, TapState> {
TapBloc._internal() : super(TapState.init()) { TapBloc._internal() : super(TapState.init()) {
on<SquareTappedEvent>(handleTap); on<SquareTappedEvent>(handleTap);
on<CancelTapEvent>(cancelTap);
} }
void handleTap(SquareTappedEvent event, Emitter<TapState> emit) { void handleTap(SquareTappedEvent event, Emitter<TapState> emit) {
@ -71,6 +72,10 @@ class TapBloc extends Bloc<TapEvent, TapState> {
pieceOnFirstTappedSquare: piece, pieceOnFirstTappedSquare: piece,
secondSquareTapped: secondTappedSquare)); secondSquareTapped: secondTappedSquare));
} }
void cancelTap(CancelTapEvent event, Emitter<TapState> emit) {
emit(TapState.init());
}
} }
abstract class TapEvent {} abstract class TapEvent {}
@ -82,6 +87,8 @@ class SquareTappedEvent extends TapEvent {
SquareTappedEvent({required this.tapped, required this.pieceOnSquare}); SquareTappedEvent({required this.tapped, required this.pieceOnSquare});
} }
class CancelTapEvent extends TapEvent {}
class TapState { class TapState {
ChessCoordinate? firstSquareTapped; ChessCoordinate? firstSquareTapped;
ChessPiece? pieceOnFirstTappedSquare; ChessPiece? pieceOnFirstTappedSquare;