Add chess bloc.
This commit is contained in:
parent
e20573e68e
commit
f7701138a4
52
lib/chess_bloc/chess_bloc.dart
Normal file
52
lib/chess_bloc/chess_bloc.dart
Normal file
@ -0,0 +1,52 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'package:mchess/chessapp/chess_utils.dart';
|
||||
import 'package:mchess/chessapp/chess_square.dart';
|
||||
|
||||
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
||||
static final ChessBloc _instance = ChessBloc._internal();
|
||||
|
||||
ChessBloc._internal() : super(ChessBoardState.init()) {
|
||||
on<PieceMoved>(moveHandler);
|
||||
}
|
||||
|
||||
factory ChessBloc.getInstance() {
|
||||
return ChessBloc();
|
||||
}
|
||||
|
||||
factory ChessBloc() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
FutureOr<void> moveHandler(
|
||||
PieceMoved event,
|
||||
Emitter<ChessBoardState> emit,
|
||||
) {
|
||||
emit(ChessBoardState(
|
||||
!state.flipped,
|
||||
ChessColor.black,
|
||||
state.position,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ChessEvent {}
|
||||
|
||||
class PieceMoved extends ChessEvent {}
|
||||
|
||||
class BoardFlippedEvent extends ChessEvent {}
|
||||
|
||||
class ChessBoardState {
|
||||
final bool flipped;
|
||||
final ChessColor turnColor;
|
||||
final Map<ChessCoordinate, ChessPiece> position;
|
||||
|
||||
ChessBoardState(this.flipped, this.turnColor, this.position);
|
||||
|
||||
ChessBoardState.init()
|
||||
: flipped = false,
|
||||
turnColor = ChessColor.white,
|
||||
position = {};
|
||||
}
|
Loading…
Reference in New Issue
Block a user