Fix routing again

This commit is contained in:
Marco 2023-12-27 15:46:15 +01:00
parent 289ec6db26
commit cdc0144e39
7 changed files with 71 additions and 56 deletions

View File

@ -1,6 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:go_router/go_router.dart';
import 'package:mchess/chess/chess_app.dart'; import 'package:mchess/chess/chess_app.dart';
void main() { void main() {
GoRouter.optionURLReflectsImperativeAPIs = true;
setUrlStrategy(const PathUrlStrategy());
runApp(const ChessApp()); runApp(const ChessApp());
} }

View File

@ -5,9 +5,9 @@ import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess/chess_board.dart'; import 'package:mchess/chess/chess_board.dart';
import 'package:mchess/chess_bloc/promotion_bloc.dart'; import 'package:mchess/chess_bloc/promotion_bloc.dart';
import 'package:mchess/connection/ws_connection.dart';
import 'package:mchess/utils/chess_utils.dart'; import 'package:mchess/utils/chess_utils.dart';
import 'package:mchess/utils/widgets/promotion_dialog.dart'; import 'package:mchess/utils/widgets/promotion_dialog.dart';
import 'package:universal_platform/universal_platform.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
class ChessGame extends StatefulWidget { class ChessGame extends StatefulWidget {
@ -32,7 +32,19 @@ class _ChessGameState extends State<ChessGame> {
@override @override
Widget build(BuildContext context) { 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( return Scaffold(
floatingActionButton: fltnBtn,
body: Center( body: Center(
child: Container( child: Container(
margin: const EdgeInsets.all(10), margin: const EdgeInsets.all(10),
@ -52,13 +64,6 @@ class _ChessGameState extends State<ChessGame> {
), ),
), ),
), ),
floatingActionButton: FloatingActionButton(
onPressed: () {
ServerConnection.getInstance().disconnectExistingConnection();
GoRouter.of(context).goNamed('lobbySelector');
},
child: const Icon(Icons.cancel),
),
); );
} }

View File

@ -30,7 +30,6 @@ class _HostGameWidgetState extends State<HostGameWidget> {
value?.store(); value?.store();
}); });
connectToWebsocket(registerResponse); connectToWebsocket(registerResponse);
super.initState(); super.initState();
} }
@ -70,7 +69,7 @@ class _HostGameWidgetState extends State<HostGameWidget> {
listener: (context, state) { listener: (context, state) {
// We wait for our opponent to connect // We wait for our opponent to connect
if (state.opponentConnected) { if (state.opponentConnected) {
context.goNamed('game', extra: chessGameArgs); context.pushReplacement('/game', extra: chessGameArgs);
} }
}, },
child: Column( child: Column(
@ -95,12 +94,6 @@ class _HostGameWidgetState extends State<HostGameWidget> {
}, },
), ),
), ),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.cancel),
onPressed: () {
context.pop();
},
),
); );
} }
@ -129,7 +122,7 @@ class _HostGameWidgetState extends State<HostGameWidget> {
Future.delayed(const Duration(seconds: 2), () { Future.delayed(const Duration(seconds: 2), () {
ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar); ScaffoldMessenger.of(context).showSnackBar(snackBar);
context.push('/'); // We go back to the lobby selector context.goNamed('lobbySelector'); // We go back to the lobby selector
}); });
return null; return null;
} }

View File

@ -19,7 +19,7 @@ class LobbySelector extends StatefulWidget {
class _LobbySelectorState extends State<LobbySelector> { class _LobbySelectorState extends State<LobbySelector> {
final buttonStyle = const ButtonStyle(); final buttonStyle = const ButtonStyle();
final myController = TextEditingController(); final phraseController = TextEditingController();
late Future<PlayerInfo?> joinGameFuture; late Future<PlayerInfo?> joinGameFuture;
@override @override
@ -37,9 +37,7 @@ class _LobbySelectorState extends State<LobbySelector> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () => buildJoinOrHostDialog(context),
buildJoinOrHostDialog(context);
},
child: const Row( child: const Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
@ -67,19 +65,18 @@ class _LobbySelectorState extends State<LobbySelector> {
actions: <Widget>[ actions: <Widget>[
TextButton( TextButton(
child: const Text('Cancel'), child: const Text('Cancel'),
onPressed: () { onPressed: () => context.pop(),
context.pop();
},
), ),
TextButton( TextButton(
child: const Text('Host'), child: const Text('Host'),
onPressed: () { onPressed: () {
context.push('/host'); //replaces joinOrHostDialog context.pop(); //close dialog before going to host
}, context.goNamed('host');
), }),
TextButton( TextButton(
child: const Text('Join'), child: const Text('Join'),
onPressed: () { onPressed: () {
context.pop(); //close dialog before going to next dialog
buildEnterPassphraseDialog(context); buildEnterPassphraseDialog(context);
}, },
), ),
@ -97,15 +94,18 @@ class _LobbySelectorState extends State<LobbySelector> {
return AlertDialog( return AlertDialog(
title: const Text('Enter the passphrase here:'), title: const Text('Enter the passphrase here:'),
content: TextField( content: TextField(
controller: myController, controller: phraseController,
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Enter passphrase here', hintText: 'Enter passphrase here',
suffixIcon: IconButton( suffixIcon: IconButton(
onPressed: () { onPressed: () {
joinGameFuture = joinPrivateGame(); joinGameFuture = joinPrivateGame();
joinGameFuture.then((value) { joinGameFuture.then((value) {
if (value == null) return; if (value != null) {
switchToGame(value); phraseController.clear();
context.pop();
switchToGame(value);
}
}); });
}, },
icon: const Icon(Icons.check), icon: const Icon(Icons.check),
@ -146,7 +146,7 @@ class _LobbySelectorState extends State<LobbySelector> {
ScaffoldMessenger.of(context).showSnackBar(snackBar); ScaffoldMessenger.of(context).showSnackBar(snackBar);
} }
context.pushReplacement('/game', extra: chessGameArgs); context.goNamed('game', extra: chessGameArgs);
} }
Future<PlayerInfo?> joinPrivateGame() async { Future<PlayerInfo?> joinPrivateGame() async {
@ -155,7 +155,7 @@ class _LobbySelectorState extends State<LobbySelector> {
// server expects us to send the passphrase // server expects us to send the passphrase
var info = PlayerInfo( var info = PlayerInfo(
playerID: null, lobbyID: null, passphrase: myController.text); playerID: null, lobbyID: null, passphrase: phraseController.text);
if (kDebugMode) { if (kDebugMode) {
addr = 'http://localhost:8080/api/joinPrivate'; addr = 'http://localhost:8080/api/joinPrivate';