flutter_stateless_chessboard 1.1.2 flutter_stateless_chessboard: ^1.1.2 copied to clipboard
A Chessboard Widget for Flutter. This package provides just the chessboard. The game logic can be implemented using chess library.
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_stateless_chessboard/flutter_stateless_chessboard.dart';
import 'utils.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Random Chess App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
@override
Widget build(BuildContext context) {
final viewport = MediaQuery.of(context).size;
final size = min(viewport.height, viewport.width);
return Scaffold(
appBar: AppBar(
title: Text("Random Chess"),
),
body: Center(
child: Chessboard(
fen: _fen,
size: size,
orientation: BoardColor.WHITE,
onMove: (move) {
final nextFen = makeMove(_fen, {
'from': move.from,
'to': move.to,
'promotion': 'q',
});
if (nextFen != null) {
setState(() {
_fen = nextFen;
});
Future.delayed(Duration(milliseconds: 300)).then((_) {
final nextMove = getRandomMove(_fen);
if (nextMove != null) {
setState(() {
_fen = makeMove(_fen, nextMove) ?? _fen;
});
}
});
}
},
),
),
);
}
}