Coder Social home page Coder Social logo

varunpvp / flutter_chessboard Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 17.0 1.53 MB

Chessboard Widget for Flutter

Home Page: https://pub.dev/packages/flutter_stateless_chessboard

License: MIT License

Dart 100.00%
chessboard chess-game flutter flutter-widget flutter-package chess chessboard-widget fen dart board chess-application

flutter_chessboard's Introduction

flutter_stateless_chessboard

A Stateless Chessboard Widget for Flutter. This package provides just the chessboard. The game logic can be implemented using chess library. Check example/main.dart file, for implementing game logic.

Simple Chess App]

Using the Chessboard

To use Chessboard widget, add flutter_stateless_chessboard as a dependency in your pubspec.yaml

Simple Example

void main() {
  runApp(
    new MaterialApp(
      home: Scaffold(
        body: Center(
          child: Chessboard(
            size: 300,
            fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
            onMove: (move) {
              // optional
              // TODO: process the move
              print("move from ${move.from} to ${move.to}");
            },
            orientation: BoardColor.BLACK, // optional
            lightSquareColor: Color.fromRGBO(240, 217, 181, 1), // optional
            darkSquareColor: Color.fromRGBO(181, 136, 99, 1), // optional
          ),
        ),
      ),
    ),
  );
}

Handling Promotion

For handling promotion. You can implement onPromote param. And return the PieceType you want to promote to. See below example.

Chessboard(
  fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
  size: 400,
  onMove: ...,
  onPromote: () {
    return showDialog<PieceType>(
      context: context,
      builder: (_) {
        return AlertDialog(
          title: Text('Promotion'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ListTile(
                title: Text("Queen"),
                onTap: () => navigator.pop(PieceType.QUEEN),
              ),
              ListTile(
                title: Text("Rook"),
                onTap: () => navigator.pop(PieceType.ROOK),
              ),
              ListTile(
                title: Text("Bishop"),
                onTap: () => navigator.pop(PieceType.BISHOP),
              ),
              ListTile(
                title: Text("Knight"),
                onTap: () => navigator.pop(PieceType.KNIGHT),
              ),
            ],
          ),
        );
      },
    );
  },
);

Handling Promotion

Building Custom Pieces

By default, library uses chess_vectors_flutter for pieces. But you can build your own piece widget by implementing buildPiece param. See below example.

Chessboard(
  fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
  size: 400,
  buildPiece: (piece, size) {
    if (piece == Piece.WHITE_PAWN) {
      return Icon(
        Icons.person,
        size: size,
        color: Colors.white,
      );
    } else if (piece == Piece.BLACK_PAWN) {
      return Icon(
        Icons.person,
        size: size,
        color: Colors.black,
      );
    }
  },
);

If you don't return widget for a PieceType default widget will be rendered. This is how the above Chessboard will look.

Custom Piece Build

Parameters

fen:

fen that should be show on the board (example rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1)

size:

Size of the chessboard widget

onMove (optional):

Called when a move is made on the board. Passing a ShortMove(from, to, promotion).

orientation (optional):

Specify orientation of the chessboard.

lightSquareColor (optional):

color of light square on chessboard.

darkSquareColor (optional):

color of dart square on chessboard.

onPromote (optional):

handle piece promotion

buildPiece (optional):

handle building of custom piece

flutter_chessboard's People

Contributors

varunpvp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flutter_chessboard's Issues

The widget is broken following README

I tried the widget today, and with the code shown in instruction, the widget is broken and cannot be built.

I found out that the chess_piece.dart expected piece.toString() to returns something like "wr", but instead, maybe due to a change in the chess package, it received "Color.WHITEr".

Therefore, I made a local copy of the widget, and then, in utils.dart, getPieceMap function, I changed from:
map[square] = Piece(piece.type.toString(), piece.color.toString()); to map[square] = Piece(piece.type.toString(), piece.color == ch.Color.BLACK ? 'b' : 'w');

And the widget works perfectly afterward!

Thank you for the widget, please check this issue and fix it (more properly, I assume) for others!

Support inactive squares

Add support for inactive squares. Inactive square is a square is not considered as a valid square to move. Or go over.

enhancement

for indicating that a player tap a piece or square you used inkwell so it will be better to add a splash color parameter in order player pay attention that they tap a particular piece correctly

fpdart issues

When I build this, I get the following errors arising from the inclusion of fpdart:

Resolving dependencies...
> async 2.11.0 (was 2.8.2)
> boolean_selector 2.1.1 (was 2.1.0)
> characters 1.3.0 (was 1.2.0)
  chess 0.7.0 (0.8.1 available)
  chess_vectors_flutter 1.0.16 (1.1.0 available)
> clock 1.1.1 (was 1.1.0)
> collection 1.18.0 (was 1.15.0) (1.19.0 available)
> fake_async 1.3.1 (was 1.2.0)
  fpdart 0.0.12 (1.1.0 available)
> matcher 0.12.16 (was 0.12.11) (0.12.16+1 available)
+ material_color_utilities 0.5.0 (0.12.0 available)
> meta 1.10.0 (was 1.7.0) (1.15.0 available)
> path 1.8.3 (was 1.8.0) (1.9.0 available)
  provider 6.0.1 (6.1.2 available)
  quiver 3.0.1+1 (3.2.1 available)
> source_span 1.10.0 (was 1.8.1)
> stack_trace 1.11.1 (was 1.10.0)
> stream_channel 2.1.2 (was 2.1.0)
> string_scanner 1.2.0 (was 1.1.0) (1.3.0 available)
> term_glyph 1.2.1 (was 1.2.0)
> test_api 0.6.1 (was 0.4.3) (0.7.3 available)
  tuple 2.0.0 (2.0.2 available)
> vector_math 2.1.4 (was 2.1.1)
+ web 0.3.0 (0.5.1 available)
These packages are no longer being depended on:
- charcode 1.3.1
- typed_data 1.3.0
Changed 20 dependencies!
14 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
This application is not configured to build on the web.
To add web support to a project, run `flutter create .`.
Launching example\lib\main.dart on Edge in debug mode...
Waiting for connection from debug service on Edge...
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/applicative.dart:13:10: Error: The class 'Functor2' can't be used as a mixin because it extends a class other than 'Object'.
    with Functor2<G, A, B> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/monad.dart:24:10: Error: The class 'Applicative2' can't be used as a mixin because it extends a class other than 'Object'.
    with Applicative2<KT, A, B> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/either.dart:28:9: Error: The class 'Monad2' can't be used as a mixin because it extends a class other than 'Object'.
        Monad2<_EitherHKT, L, R>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/either.dart:29:9: Error: The class 'Foldable2' can't be used as a mixin because it extends a class other than 'Object'.
        Foldable2<_EitherHKT, L, R>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/alt.dart:12:59: Error: The class 'Functor2' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Alt2<KT, A, B> extends HKT2<KT, A, B> with Functor2<KT, A, B> {
                                                          ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/either.dart:30:9: Error: The class 'Alt2' can't be used as a mixin because it extends a class other than 'Object'.
        Alt2<_EitherHKT, L, R>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/extend.dart:19:10: Error: The class 'Functor2' can't be used as a mixin because it extends a class other than 'Object'.
    with Functor2<KT, A, B> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/either.dart:31:9: Error: The class 'Extend2' can't be used as a mixin because it extends a class other than 'Object'.
        Extend2<_EitherHKT, L, R> {
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/applicative.dart:4:57: Error: The class 'Functor' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Applicative<G, A> extends HKT<G, A> with Functor<G, A> {
                                                        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/monad.dart:4:53: Error: The class 'Applicative' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Monad<KT, A> extends HKT<KT, A> with Applicative<KT, A> {
                                                    ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/io.dart:10:41: Error: The class 'Monad' can't be used as a mixin because it extends a class other than 'Object'.
class IO<A> extends HKT<_IOHKT, A> with Monad<_IOHKT, A> {
                                        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/io_either.dart:20:10: Error: The class 'Monad2' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad2<_IOEitherHKT, L, R>, Alt2<_IOEitherHKT, L, R> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/io_either.dart:20:38: Error: The class 'Alt2' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad2<_IOEitherHKT, L, R>, Alt2<_IOEitherHKT, L, R> {
                                     ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/option.dart:64:9: Error: The class 'Monad' can't be used as a mixin because it extends a class other than 'Object'.
        Monad<_OptionHKT, T>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/option.dart:65:9: Error: The class 'Foldable' can't be used as a mixin because it extends a class other than 'Object'.
        Foldable<_OptionHKT, T>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/alt.dart:8:51: Error: The class 'Functor' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Alt<KT, A> extends HKT<KT, A> with Functor<KT, A> {
                                                  ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/option.dart:66:9: Error: The class 'Alt' can't be used as a mixin because it extends a class other than 'Object'.
        Alt<_OptionHKT, T>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/extend.dart:6:54: Error: The class 'Functor' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Extend<KT, A> extends HKT<KT, A> with Functor<KT, A> {
                                                     ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/option.dart:67:9: Error: The class 'Extend' can't be used as a mixin because it extends a class other than 'Object'.
        Extend<_OptionHKT, T>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/filterable.dart:8:58: Error: The class 'Functor' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Filterable<KT, A> extends HKT<KT, A> with Functor<KT, A> {
                                                         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/option.dart:68:9: Error: The class 'Filterable' can't be used as a mixin because it extends a class other than 'Object'.
        Filterable<_OptionHKT, T> {
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/reader.dart:9:55: Error: The class 'Monad2' can't be used as a mixin because it extends a class other than 'Object'.
class Reader<R, A> extends HKT2<ReaderHKT, R, A> with Monad2<ReaderHKT, R, A> {
                                                      ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/state.dart:15:54: Error: The class 'Monad2' can't be used as a mixin because it extends a class other than 'Object'.
class State<S, A> extends HKT2<_StateHKT, S, A> with Monad2<_StateHKT, S, A> {
                                                     ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/state_async.dart:18:10: Error: The class 'Monad2' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad2<_StateAsyncHKT, S, A> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/task.dart:9:45: Error: The class 'Monad' can't be used as a mixin because it extends a class other than 'Object'.
class Task<A> extends HKT<_TaskHKT, A> with Monad<_TaskHKT, A> {
                                            ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/task_either.dart:12:10: Error: The class 'Monad2' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad2<_TaskEitherHKT, L, R>, Alt2<_TaskEitherHKT, L, R> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/task_either.dart:12:40: Error: The class 'Alt2' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad2<_TaskEitherHKT, L, R>, Alt2<_TaskEitherHKT, L, R> {
                                       ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/task_option.dart:15:10: Error: The class 'Monad' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad<_TaskOptionHKT, R>, Alt<_TaskOptionHKT, R> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/task_option.dart:15:36: Error: The class 'Alt' can't be used as a mixin because it extends a class other than 'Object'.
    with Monad<_TaskOptionHKT, R>, Alt<_TaskOptionHKT, R> {
                                   ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/tuple.dart:25:9: Error: The class 'Functor2' can't be used as a mixin because it extends a class other than 'Object'.
        Functor2<_Tuple2HKT, T1, T2>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/tuple.dart:26:9: Error: The class 'Extend2' can't be used as a mixin because it extends a class other than 'Object'.
        Extend2<_Tuple2HKT, T1, T2>,
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/tuple.dart:27:9: Error: The class 'Foldable2' can't be used as a mixin because it extends a class other than 'Object'.
        Foldable2<_Tuple2HKT, T1, T2> {
        ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/commutative_monoid.dart:9:10: Error: The class 'CommutativeSemigroup' can't be used as a mixin because it extends a class other than 'Object'.
    with CommutativeSemigroup<T> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/semilattice.dart:19:52: Error: The class 'CommutativeSemigroup' can't be used as a mixin because it extends a class other than 'Object'.
abstract class Semilattice<T> extends Band<T> with CommutativeSemigroup<T> {
                                                   ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/bounded_semilattice.dart:12:10: Error: The class 'Semilattice' can't be used as a mixin because it extends a class other than 'Object'.
    with Semilattice<T> {
         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/fpdart-0.0.12/lib/src/typeclass/commutative_group.dart:7:58: Error: The class 'CommutativeMonoid' can't be used as a mixin because it extends a class other than 'Object'.
abstract class CommutativeGroup<T> extends Group<T> with CommutativeMonoid<T> {
                                                         ^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.