Make a lot of changes. CAUTION: Board flips on purpose on every move.

This commit is contained in:
Marco 2022-11-13 01:03:47 +01:00
parent f7701138a4
commit 94b7c227c9
5 changed files with 75 additions and 27 deletions

View File

@ -1,4 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'chess_board.dart'; import 'chess_board.dart';
@ -27,9 +29,15 @@ class ChessApp extends StatelessWidget {
fit: BoxFit.contain, fit: BoxFit.contain,
child: Container( child: Container(
margin: const EdgeInsets.all(20), margin: const EdgeInsets.all(20),
child: const ChessBoard( child: BlocProvider(
flipped: false, create: (_) => ChessBloc.getInstance(),
), child: BlocBuilder<ChessBloc, ChessBoardState>(
builder: (context, state) {
return ChessBoard(
bState: state,
);
},
)),
), ),
), ),
), ),

View File

@ -1,24 +1,17 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'chess_square.dart'; import 'chess_square.dart';
import 'chess_utils.dart'; import 'chess_utils.dart';
class ChessBoard extends StatefulWidget { class ChessBoard extends StatelessWidget {
final bool flipped; final ChessBoardState bState;
final List<ChessSquare> squares;
const ChessBoard({required this.flipped, super.key}); ChessBoard._({required this.bState, required this.squares});
@override factory ChessBoard({required ChessBoardState bState}) {
State<StatefulWidget> createState() => ChessBoardState(); List<ChessSquare> squares = List.empty(growable: true);
}
class ChessBoardState extends State<ChessBoard> {
var squares = <ChessSquare>[];
ChessColor turnColor = ChessColor.white;
@override
Widget build(BuildContext context) {
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
var column = (i % 8) + 1; var column = (i % 8) + 1;
var row = (i ~/ 8) + 1; var row = (i ~/ 8) + 1;
@ -30,14 +23,22 @@ class ChessBoardState extends State<ChessBoard> {
); );
} }
return ChessBoard._(bState: bState, squares: squares);
}
@override
Widget build(BuildContext context) {
_placePieces(); _placePieces();
return Column( return Column(
children: _buildBoard(widget.flipped), children: _buildBoard(bState.flipped),
); );
} }
void _placePieces() {} void _placePieces() {
squares[0].containedPiece =
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
}
Row _buildChessRow(int rowNo, bool flipped) { Row _buildChessRow(int rowNo, bool flipped) {
if (!flipped) { if (!flipped) {

View File

@ -1,14 +1,15 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'chess_utils.dart'; import 'chess_utils.dart';
class ChessSquare extends StatefulWidget { class ChessSquare extends StatefulWidget {
final ChessCoordinate coordinate; final ChessCoordinate coordinate;
final ChessPiece? containedPiece; late ChessPiece? containedPiece;
static const double pieceWidth = 200; static const double pieceWidth = 200;
static const double pieceHeight = 200; static const double pieceHeight = 200;
const ChessSquare({required this.coordinate, this.containedPiece, super.key}); ChessSquare({required this.coordinate, this.containedPiece, super.key});
@override @override
State<StatefulWidget> createState() => _ChessSquareState(); State<StatefulWidget> createState() => _ChessSquareState();
@ -53,16 +54,19 @@ class _ChessSquareState extends State<ChessSquare> {
child: widget.containedPiece ?? Container(), child: widget.containedPiece ?? Container(),
onDragCompleted: () { onDragCompleted: () {
setState(() { setState(() {
// widget.containedPiece = null; widget.containedPiece = null;
}); });
}, },
), ),
); );
}, },
onWillAccept: (move) { onWillAccept: (move) {
return false; print('onWillAccept');
return true;
},
onAccept: (move) {
ChessBloc().add(PieceMoved());
}, },
onAccept: (move) {},
); );
} }

View File

@ -9,6 +9,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.10.0" version: "2.10.0"
bloc:
dependency: transitive
description:
name: bloc
sha256: bd4f8027bfa60d96c8046dec5ce74c463b2c918dce1b0d36593575995344534a
url: "https://pub.dev"
source: hosted
version: "8.1.0"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
@ -62,6 +70,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_bloc:
dependency: "direct main"
description:
name: flutter_bloc
sha256: "890c51c8007f0182360e523518a0c732efb89876cb4669307af7efada5b55557"
url: "https://pub.dev"
source: hosted
version: "8.1.1"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:
@ -123,6 +139,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.8.0" version: "1.8.0"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
path: path:
dependency: transitive dependency: transitive
description: description:
@ -155,6 +179,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.1.0" version: "5.1.0"
provider:
dependency: transitive
description:
name: provider
sha256: e1e7413d70444ea3096815a60fe5da1b11bda8a9dc4769252cc82c53536f8bcc
url: "https://pub.dev"
source: hosted
version: "6.0.4"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter

View File

@ -36,6 +36,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
flutter_bloc: ^8.1.1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
@ -60,9 +61,11 @@ flutter:
uses-material-design: true uses-material-design: true
# To add assets to your application, add an assets section, like this: # To add assets to your application, add an assets section, like this:
# assets: assets:
# - images/a_dot_burr.jpeg - assets/
# - images/a_dot_ham.jpeg - assets/pieces/
- assets/pieces/black/
- assets/pieces/white/
# An image asset can refer to one or more resolution-specific "variants", see # An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware # https://flutter.dev/assets-and-images/#resolution-aware