import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; import 'package:mchess/utils/chess_utils.dart'; class PromotionBloc extends Bloc { static final PromotionBloc _instance = PromotionBloc._internal(); static late ChessMove move; PromotionBloc._internal() : super(PromotionState.init()) { on(promotionMoveHandler); on(pieceChosenHandler); } void promotionMoveHandler( PawnMovedToPromotionField event, Emitter emit, ) { switch (event.colorMoved) { case ChessColor.white: if (event.move.from.row != 7) return; break; case ChessColor.black: if (event.move.from.row != 2) return; break; } move = event.move; emit(PromotionState( showPromotionDialog: true, colorMoved: event.colorMoved)); } void pieceChosenHandler( PieceChosen event, Emitter emit, ) { ChessBloc.getInstance() .add(OwnPromotionPlayed(pieceClass: event.pieceClass, move: move)); emit(PromotionState(showPromotionDialog: false, colorMoved: event.color)); } factory PromotionBloc.getInstance() { return PromotionBloc(); } factory PromotionBloc() { return _instance; } } abstract class PromotionEvent {} class PawnMovedToPromotionField extends PromotionEvent { final ChessMove move; final ChessColor colorMoved; PawnMovedToPromotionField({required this.move, required this.colorMoved}); } class PieceChosen extends PromotionEvent { final ChessPieceClass pieceClass; final ChessColor color; PieceChosen({required this.pieceClass, required this.color}); } class PromotionState { final bool showPromotionDialog; final ChessColor colorMoved; PromotionState({required this.showPromotionDialog, required this.colorMoved}); factory PromotionState.init() { return PromotionState( showPromotionDialog: false, colorMoved: ChessColor.white); } }