chessboard_js 0.0.2+1
chessboard_js: ^0.0.2+1 copied to clipboard
A chessboard widget to use with chessjs. Use chessjs for the logic of the game, and this package to display the game.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:chessboard_js/chessboard_js.dart';
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ChessGame(),
);
}
}
final moveProvider = StateProvider<String>((ref) => '');
class ChessGame extends ConsumerWidget {
@override
Widget build(BuildContext context, ScopedReader watch) {
final ChessboardController controller =
ChessboardController(onMove: (move) {
print('move: $move');
});
return Scaffold(
backgroundColor: Colors.black,
body: SizedBox.expand(
child: Theme(
data: ThemeData.dark(),
child: Row(
children: [
ChessBoard(
initialSize: 400,
minSize: 300,
controller: controller,
),
Column(
children: [
Text('Test Moves', style: TextStyle(color: Colors.white)),
Container(
width: 200,
child: TextField(
onChanged: (value) {
context.read(moveProvider).state = value;
},
),
),
TextButton(
onPressed: () {
final move = context.read(moveProvider).state;
bool moveMade = controller.makeSanMove(move);
if (!moveMade) {
showDialog(
context: context,
builder: (context) =>
AlertDialog(title: Text('Illegal Move')));
}
// print(controller.getSanHistory());
},
child: Text('Submit')),
TextButton(
onPressed: () {
if (controller.movesEnabled)
controller.disableUserMoves();
else
controller.enableUserMove();
},
child: Text('Enable/Disable Moves'))
],
)
],
),
),
),
);
}
}