GithubHelp home page GithubHelp logo

gulsenkeskin / flutter_chess_board Goto Github PK

View Code? Open in Web Editor NEW

This project forked from deven98/flutter_chess_board

0.0 0.0 0.0 3.04 MB

A Chessboard widget for Flutter.

License: BSD 2-Clause "Simplified" License

Objective-C 0.16% Kotlin 0.50% Dart 81.46% Swift 1.66% HTML 16.22%

flutter_chess_board's Introduction

flutter_chess_board

A full-fledged Chessboard widget for Flutter. Includes the Chessboard widget, stores game state, and includes a controller to check and manipulate game state.

Full support for formats like PGN, FEN as well as ASCII. Supports multiple colors and displaying arrows on the board. Easy access to making / undoing moves on the board.

Note: v1.0+ is a major refactor and the earlier architecture which used scoped_model internally is no longer in use. The ChessBoardController now stores the state of the game as well as contains options to change and check state. Arrows on the board are now supported.

Setting Up

Add a ChessBoard widget and attach a ChessBoardController to it.

Let's also set the board color and specify the board orientation (default is towards white).

import 'package:flutter/material.dart';
import 'package:flutter_chess_board/flutter_chess_board.dart';
        
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  ChessBoardController controller = ChessBoardController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Chess Demo'),
      ),
      body: Center(
        child: ChessBoard(
          controller: controller,
          boardColor: BoardColor.orange,
          boardOrientation: PlayerColor.white,
        ),
      ),
    );
  }
}

Let's try to display arrows on the board and also display moves of the current game:

import 'package:flutter/material.dart';
import 'package:flutter_chess_board/flutter_chess_board.dart';
        
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  ChessBoardController controller = ChessBoardController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Chess Demo'),
      ),
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: ChessBoard(
                controller: controller,
                boardColor: BoardColor.orange,
                arrows: [
                  BoardArrow(
                    from: 'd2',
                    to: 'd4',
                    color: Colors.red.withOpacity(0.5),
                  ),
                ],
                boardOrientation: PlayerColor.white,
              ),
            ),
          ),
          Expanded(
            child: ValueListenableBuilder<Chess>(
              valueListenable: controller,
              builder: (context, game, _) {
                return Text(
                  controller.getSan().fold(
                        '',
                        (previousValue, element) =>
                            previousValue + '\n' + (element ?? ''),
                      ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

This gives us:

alt text

Watch for game changes

You can listen to game state changes (move, half moves, etc) using either a listener or a ValueListenableBuilder:

  ChessBoardController controller = ChessBoardController();
  
  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      // Do something here
    });
  }
  ValueListenableBuilder<Chess>(
    valueListenable: controller,
    builder: (context, game, _) {
      // Build on new game state
    },
  ),

Checking Game States

The ChessBoardController holds the game itself and you can do various kinds of things using it:

Load New Game

controller.loadPgn('demo PGN')

controller.loadFen('demo FEN')

Make Move:

controller.makeMove(from: 'd2', to: 'd4')

Undo Move:

controller.undoMove()

Check states:

controller.isCheckMate()

controller.isDraw()

controller.isStaleMate()

controller.isThreefoldRepetition

...and more...

Move Count

controller.getMoveCount()

controller.getHalfMoveCount()

Other Board Types

alt text

alt text

alt text

Import the package

To use this package, add chess_board as a dependency in your pubspec.yaml

flutter_chess_board's People

Contributors

deven98 avatar gulsenkeskin avatar

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.