import 'dart:async'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'chess_events.dart'; import 'package:mchess/chessapp/chess_utils.dart'; class ChessBloc extends Bloc { static final ChessBloc _instance = ChessBloc._internal(); ChessBloc._internal() : super(ChessBoardState.init()) { on(moveHandler); on(preCheckHandler); } factory ChessBloc.getInstance() { return ChessBloc(); } factory ChessBloc() { return _instance; } FutureOr moveHandler( PieceMoved event, Emitter emit, ) { Map newPosition = {}; newPosition[event.endSquare] = state.position[event.startSquare]!; newPosition[event.startSquare] = const ChessPiece.none(); print('emitting new state with position $newPosition'); emit(ChessBoardState( state.flipped, ChessColor.black, newPosition, )); } void preCheckHandler( PreCheckMove event, Emitter emit, ) { print('Pretending to check a move before you drop a piece'); } } class ChessBoardState { final bool flipped; final ChessColor turnColor; final Map position; ChessBoardState._(this.flipped, this.turnColor, this.position); factory ChessBoardState( bool flipped, ChessColor turnColor, Map position, ) { return ChessBoardState._(flipped, turnColor, position); } factory ChessBoardState.init() { bool flipped = false; ChessColor turnColor = ChessColor.white; Map position = {}; position[ChessCoordinate(1, 1)] = ChessPiece(ChessPieceName.blackKing, ChessColor.black); return ChessBoardState._(flipped, turnColor, position); } }