一个国际象棋棋盘,并非用于下棋,而是用于定义自定义的国际象棋棋盘位置并获取其福赛特-爱德华兹记数法 (FEN) 代码。
特点
使用此小部件,您可以:
- 通过单击即可设置每个单元格的值(首先选择编辑的棋子类型/或垃圾桶)
- 清空棋盘/设置为标准位置
- 使用自定义位置加载小部件,然后在需要时将其重置为初始位置
- 获取 FEN 值/从 FEN 值设置棋盘。棋盘会自动调整其布局:无论是纵向还是横向。
另外,您必须手动定义所有标签文本。
入门
- 确保使用 `Labels` 类的实例为所有按钮文本提供值,
- 您必须通过将初始位置传递给构造函数来创建一个 `PositionController`:此控制器中的位置值(`currentPosition` 属性)将使用最新注册的位置进行更新。
用法
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _controller = PositionController(
'rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2');
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: [
SizedBox(
height: 600.0,
child: EditableChessBoard(
boardSize: 400.0,
controller: _controller,
labels: Labels(
playerTurnLabel: 'Player turn :',
whitePlayerLabel: 'White',
blackPlayerLabel: 'Black',
availableCastlesLabel: 'Available castles :',
whiteOOLabel: 'White O-O',
whiteOOOLabel: 'White O-O-O',
blackOOLabel: 'Black O-O',
blackOOOLabel: 'Black O-O-O',
enPassantLabel: 'En passant square :',
drawHalfMovesCountLabel: 'Draw half moves count : ',
moveNumberLabel: 'Move number : ',
submitFieldLabel: 'Validate field',
currentPositionLabel: 'Current position: ',
copyFenLabel: 'Copy position',
pasteFenLabel: 'Paste position',
resetPosition: 'Reset position',
standardPosition: 'Standard position',
erasePosition: 'Erase position',
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
final isEmptyBoard =
_controller.currentPosition.split(' ')[0] ==
"8/8/8/8/8/8/8/8";
final isValidPosition = !isEmptyBoard &&
chess.Chess.validate_fen(
_controller.currentPosition)['valid'] ==
true;
final message = isValidPosition
? "Valid position"
: "Illegal position !";
final snackBar = SnackBar(
content: Text(message),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child: const Text('Validate position'),
),
],
)
],
),
),
);
}
}
