import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:mchess/connection/ws_connection.dart'; import 'package:mchess/connection_cubit/connection_cubit.dart'; import 'package:mchess/utils/passpharse.dart'; class LobbySelector extends StatefulWidget { const LobbySelector({super.key}); @override State createState() => _LobbySelectorState(); } class _LobbySelectorState extends State { final buttonStyle = const ButtonStyle(); final phraseController = TextEditingController(); late Future joinGameFuture; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () => buildJoinOrCreateDialog(context), child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.lock), SizedBox( width: 10, ), Text('Private game') ], ), ), ], ), ), ); } Future buildJoinOrCreateDialog(BuildContext context) { //TODO: find a better place to disconnect old websocket connection ServerConnection.getInstance().disconnectExistingConnection(); return showDialog( context: context, builder: (BuildContext context) { return Scaffold( backgroundColor: Colors.transparent, body: AlertDialog( title: const Text('Create or join?'), actions: [ TextButton( child: const Text('Cancel'), onPressed: () => context.pop(), ), TextButton( child: const Text('Create'), onPressed: () { context.pop(); //close dialog before going to createGame context.goNamed('createGame'); }), TextButton( child: const Text('Join'), onPressed: () { context.pop(); //close dialog before going to next dialog buildEnterPassphraseDialog(context); }, ), ], ), ); }, ); } Future buildEnterPassphraseDialog(BuildContext context) { return showDialog( context: context, builder: (BuildContext context) { return ScaffoldMessenger( child: Builder(builder: (builderContext) { return Scaffold( backgroundColor: Colors.transparent, body: AlertDialog( title: const Text('Enter the passphrase here:'), content: TextField( controller: phraseController, onSubmitted: (val) { submitPassphrase(builderContext); }, decoration: InputDecoration( hintText: 'Enter passphrase here', suffixIcon: IconButton( onPressed: () { submitPassphrase(builderContext); }, icon: const Icon(Icons.check), )), ), actions: [ TextButton( child: const Text('Cancel'), onPressed: () { builderContext.pop(); }, ), ], ), ); }), ); }, ); } void submitPassphrase(BuildContext ctx) { switchToGame(phraseController.text); } void switchToGame(String phrase) { ConnectionCubit.getInstance().connect( phrase, ); var urlPassphrase = phrase.phraseToURL(); context.goNamed('game', pathParameters: {'phrase': urlPassphrase}); } }