import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_position.dart'; import 'package:mchess/utils/chess_utils.dart'; class MoveHistory extends StatefulWidget { const MoveHistory({super.key}); @override State createState() => _MoveHistoryState(); } class _MoveHistoryState extends State { late List entries; @override void initState() { entries = []; super.initState(); } @override Widget build(BuildContext context) { return BlocListener( listener: (context, state) { List newEntries = []; var positionManager = ChessPositionManager.getInstance(); var allMoves = positionManager.allMoves; for (ChessMove move in allMoves) { var movedPiece = positionManager.getPieceAt(move.to); var char = pieceCharacter[ChessPieceAssetKey( pieceClass: movedPiece!.pieceClass, color: movedPiece.color.getOpposite())]; if (movedPiece.color == ChessColor.white) { newEntries.add("$char ${move.to.toAlphabetical()}"); } else { newEntries.last = "${newEntries.last}\t\t$char${move.to.toAlphabetical()}"; } } setState(() { entries = newEntries; }); }, child: ListView(children: [ for (var entry in entries) Text(entry), ]), ); } }