From 94b7c227c9906aa49413e92b28d734f7917d2cb6 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 13 Nov 2022 01:03:47 +0100 Subject: [PATCH] Make a lot of changes. CAUTION: Board flips on purpose on every move. --- lib/chessapp/chess_app.dart | 14 +++++++++++--- lib/chessapp/chess_board.dart | 33 +++++++++++++++++---------------- lib/chessapp/chess_square.dart | 14 +++++++++----- pubspec.lock | 32 ++++++++++++++++++++++++++++++++ pubspec.yaml | 9 ++++++--- 5 files changed, 75 insertions(+), 27 deletions(-) diff --git a/lib/chessapp/chess_app.dart b/lib/chessapp/chess_app.dart index 8473a57..6106daa 100644 --- a/lib/chessapp/chess_app.dart +++ b/lib/chessapp/chess_app.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'chess_board.dart'; @@ -27,9 +29,15 @@ class ChessApp extends StatelessWidget { fit: BoxFit.contain, child: Container( margin: const EdgeInsets.all(20), - child: const ChessBoard( - flipped: false, - ), + child: BlocProvider( + create: (_) => ChessBloc.getInstance(), + child: BlocBuilder( + builder: (context, state) { + return ChessBoard( + bState: state, + ); + }, + )), ), ), ), diff --git a/lib/chessapp/chess_board.dart b/lib/chessapp/chess_board.dart index 0156254..e8de47f 100644 --- a/lib/chessapp/chess_board.dart +++ b/lib/chessapp/chess_board.dart @@ -1,24 +1,17 @@ import 'package:flutter/material.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'chess_square.dart'; import 'chess_utils.dart'; -class ChessBoard extends StatefulWidget { - final bool flipped; +class ChessBoard extends StatelessWidget { + final ChessBoardState bState; + final List squares; - const ChessBoard({required this.flipped, super.key}); + ChessBoard._({required this.bState, required this.squares}); - @override - State createState() => ChessBoardState(); -} - -class ChessBoardState extends State { - var squares = []; - - ChessColor turnColor = ChessColor.white; - - @override - Widget build(BuildContext context) { + factory ChessBoard({required ChessBoardState bState}) { + List squares = List.empty(growable: true); for (int i = 0; i < 64; i++) { var column = (i % 8) + 1; var row = (i ~/ 8) + 1; @@ -30,14 +23,22 @@ class ChessBoardState extends State { ); } + return ChessBoard._(bState: bState, squares: squares); + } + + @override + Widget build(BuildContext context) { _placePieces(); 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) { if (!flipped) { diff --git a/lib/chessapp/chess_square.dart b/lib/chessapp/chess_square.dart index 23843de..fb3fbc7 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chessapp/chess_square.dart @@ -1,14 +1,15 @@ import 'package:flutter/material.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'chess_utils.dart'; class ChessSquare extends StatefulWidget { final ChessCoordinate coordinate; - final ChessPiece? containedPiece; + late ChessPiece? containedPiece; static const double pieceWidth = 200; static const double pieceHeight = 200; - const ChessSquare({required this.coordinate, this.containedPiece, super.key}); + ChessSquare({required this.coordinate, this.containedPiece, super.key}); @override State createState() => _ChessSquareState(); @@ -53,16 +54,19 @@ class _ChessSquareState extends State { child: widget.containedPiece ?? Container(), onDragCompleted: () { setState(() { - // widget.containedPiece = null; + widget.containedPiece = null; }); }, ), ); }, onWillAccept: (move) { - return false; + print('onWillAccept'); + return true; + }, + onAccept: (move) { + ChessBloc().add(PieceMoved()); }, - onAccept: (move) {}, ); } diff --git a/pubspec.lock b/pubspec.lock index 676a27d..c4be003 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -9,6 +9,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.10.0" + bloc: + dependency: transitive + description: + name: bloc + sha256: bd4f8027bfa60d96c8046dec5ce74c463b2c918dce1b0d36593575995344534a + url: "https://pub.dev" + source: hosted + version: "8.1.0" boolean_selector: dependency: transitive description: @@ -62,6 +70,14 @@ packages: description: flutter source: sdk 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: dependency: "direct dev" description: @@ -123,6 +139,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.0" + nested: + dependency: transitive + description: + name: nested + sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" + url: "https://pub.dev" + source: hosted + version: "1.0.0" path: dependency: transitive description: @@ -155,6 +179,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.1.0" + provider: + dependency: transitive + description: + name: provider + sha256: e1e7413d70444ea3096815a60fe5da1b11bda8a9dc4769252cc82c53536f8bcc + url: "https://pub.dev" + source: hosted + version: "6.0.4" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index b876c89..ee198a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,6 +36,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + flutter_bloc: ^8.1.1 dev_dependencies: flutter_test: @@ -60,9 +61,11 @@ flutter: uses-material-design: true # To add assets to your application, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg + assets: + - assets/ + - assets/pieces/ + - assets/pieces/black/ + - assets/pieces/white/ # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware