simple_chess_board 0.9.0
simple_chess_board: ^0.9.0 copied to clipboard
A simple chess board widget which do not handle game state, but with several options for board orientation, adding arrows, and others.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:simple_chess_board/models/board_arrow.dart';
import 'package:chess/chess.dart' as chesslib;
import 'package:simple_chess_board/simple_chess_board.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Simple chess board Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Simple chess board Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _chess = chesslib.Chess.fromFEN(chesslib.Chess.DEFAULT_POSITION);
var _blackAtBottom = false;
BoardArrow? _lastMoveArrowCoordinates;
late ChessBoardColors _boardColors;
@override
void initState() {
_boardColors = ChessBoardColors()
..lightSquaresColor = Colors.blue.shade200
..darkSquaresColor = Colors.blue.shade600
..coordinatesZoneColor = Colors.redAccent.shade200
..lastMoveArrowColor = Colors.cyan
..selectionHighlightColor = Colors.orange
..circularProgressBarColor = Colors.red
..coordinatesColor = Colors.green;
super.initState();
}
void tryMakingMove({required ShortMove move}) {
final success = _chess.move(<String, String?>{
'from': move.from,
'to': move.to,
'promotion': move.promotion.toNullable()?.name,
});
if (success) {
setState(() {
_lastMoveArrowCoordinates = BoardArrow(from: move.from, to: move.to);
});
}
}
Future<PieceType?> handlePromotion(BuildContext context) {
final navigator = Navigator.of(context);
return showDialog<PieceType>(
context: context,
builder: (_) {
return AlertDialog(
title: const Text('Promotion'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: const Text("Queen"),
onTap: () => navigator.pop(PieceType.queen),
),
ListTile(
title: const Text("Rook"),
onTap: () => navigator.pop(PieceType.rook),
),
ListTile(
title: const Text("Bishop"),
onTap: () => navigator.pop(PieceType.bishop),
),
ListTile(
title: const Text("Knight"),
onTap: () => navigator.pop(PieceType.knight),
),
],
),
);
},
);
}
@override
Widget build(BuildContext context) {
final boardOrientation =
_blackAtBottom ? BoardColor.black : BoardColor.white;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
IconButton(
onPressed: () {
setState(() {
_blackAtBottom = !_blackAtBottom;
});
},
icon: const Icon(Icons.swap_vert),
)
],
),
body: Center(
child: SimpleChessBoard(
chessBoardColors: _boardColors,
engineThinking: false,
fen: _chess.fen,
onMove: tryMakingMove,
orientation: boardOrientation,
whitePlayerType: PlayerType.human,
blackPlayerType: PlayerType.human,
lastMoveToHighlight: _lastMoveArrowCoordinates,
onPromote: () => handlePromotion(context),
onPromotionCommited: ({required PieceType pieceType}) =>
{print(pieceType)},
),
),
);
}
}