import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess/chess_board.dart'; import 'package:mchess/chess_bloc/promotion_bloc.dart'; import 'package:mchess/chess_bloc/tap_bloc.dart'; import 'package:mchess/utils/chess_utils.dart'; import 'package:mchess/utils/widgets/promotion_dialog.dart'; import 'package:universal_platform/universal_platform.dart'; import 'package:uuid/uuid.dart'; class ChessGame extends StatefulWidget { final UuidValue playerID; final UuidValue lobbyID; final String? passphrase; const ChessGame( {required this.playerID, required this.lobbyID, required this.passphrase, super.key}); @override State createState() => _ChessGameState(); } class _ChessGameState extends State { ChessCoordinate? tappedSquare; @override Widget build(BuildContext context) { FloatingActionButton? fltnBtn; if (UniversalPlatform.isLinux || UniversalPlatform.isMacOS || UniversalPlatform.isWindows) { fltnBtn = FloatingActionButton( child: const Icon(Icons.cancel), onPressed: () { context.pop(); }, ); } return Scaffold( floatingActionButton: fltnBtn, body: Center( child: Container( margin: const EdgeInsets.all(10), child: BlocListener( listener: (context, state) { setState(() { tappedSquare = state.firstSquareTapped; }); }, child: BlocListener( listener: (listenerContext, state) { if (state.showPromotionDialog) { promotionDialogBuilder(listenerContext, state.colorMoved); } }, child: BlocBuilder( builder: (context, state) { return ChessBoard( boardState: state, tappedSquare: tappedSquare, ); }, ), ), ), ), ), ); } Future promotionDialogBuilder(BuildContext context, ChessColor color) { return showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return PromotionDialog( sideColor: color, ); }, ); } } class ChessGameArguments { final UuidValue lobbyID; final UuidValue playerID; final String? passphrase; ChessGameArguments({ required this.lobbyID, required this.playerID, required this.passphrase, }); bool isValid() { try { lobbyID.validate(); playerID.validate(); } catch (e) { return false; } if (passphrase == null) return false; if (passphrase!.isEmpty) return false; return true; } }