From cdc0144e3903b6467feb0e7128d264b2034cce14 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 27 Dec 2023 15:46:15 +0100 Subject: [PATCH] Fix routing again --- lib/main.dart | 5 +++++ lib/pages/chess_game.dart | 21 +++++++++++------- lib/pages/host_game.dart | 11 ++-------- lib/pages/lobby_selector.dart | 34 ++++++++++++++--------------- lib/utils/chess_router.dart | 41 +++++++++++++++++++---------------- pubspec.lock | 12 ++++++++-- pubspec.yaml | 3 ++- 7 files changed, 71 insertions(+), 56 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 34e0a50..820d9ad 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,11 @@ 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'; void main() { + GoRouter.optionURLReflectsImperativeAPIs = true; + setUrlStrategy(const PathUrlStrategy()); + runApp(const ChessApp()); } diff --git a/lib/pages/chess_game.dart b/lib/pages/chess_game.dart index 7fb7d92..e7c2cdb 100644 --- a/lib/pages/chess_game.dart +++ b/lib/pages/chess_game.dart @@ -5,9 +5,9 @@ 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/connection/ws_connection.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 { @@ -32,7 +32,19 @@ class _ChessGameState extends State { @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), @@ -52,13 +64,6 @@ class _ChessGameState extends State { ), ), ), - floatingActionButton: FloatingActionButton( - onPressed: () { - ServerConnection.getInstance().disconnectExistingConnection(); - GoRouter.of(context).goNamed('lobbySelector'); - }, - child: const Icon(Icons.cancel), - ), ); } diff --git a/lib/pages/host_game.dart b/lib/pages/host_game.dart index c8076d7..9a6165c 100644 --- a/lib/pages/host_game.dart +++ b/lib/pages/host_game.dart @@ -30,7 +30,6 @@ class _HostGameWidgetState extends State { value?.store(); }); connectToWebsocket(registerResponse); - super.initState(); } @@ -70,7 +69,7 @@ class _HostGameWidgetState extends State { listener: (context, state) { // We wait for our opponent to connect if (state.opponentConnected) { - context.goNamed('game', extra: chessGameArgs); + context.pushReplacement('/game', extra: chessGameArgs); } }, child: Column( @@ -95,12 +94,6 @@ class _HostGameWidgetState extends State { }, ), ), - floatingActionButton: FloatingActionButton( - child: const Icon(Icons.cancel), - onPressed: () { - context.pop(); - }, - ), ); } @@ -129,7 +122,7 @@ class _HostGameWidgetState extends State { Future.delayed(const Duration(seconds: 2), () { ScaffoldMessenger.of(context).clearSnackBars(); 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; } diff --git a/lib/pages/lobby_selector.dart b/lib/pages/lobby_selector.dart index 1afa481..a1a4d0a 100644 --- a/lib/pages/lobby_selector.dart +++ b/lib/pages/lobby_selector.dart @@ -19,7 +19,7 @@ class LobbySelector extends StatefulWidget { class _LobbySelectorState extends State { final buttonStyle = const ButtonStyle(); - final myController = TextEditingController(); + final phraseController = TextEditingController(); late Future joinGameFuture; @override @@ -37,9 +37,7 @@ class _LobbySelectorState extends State { mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( - onPressed: () { - buildJoinOrHostDialog(context); - }, + onPressed: () => buildJoinOrHostDialog(context), child: const Row( mainAxisSize: MainAxisSize.min, children: [ @@ -67,19 +65,18 @@ class _LobbySelectorState extends State { actions: [ TextButton( child: const Text('Cancel'), - onPressed: () { - context.pop(); - }, + onPressed: () => context.pop(), ), TextButton( - child: const Text('Host'), - onPressed: () { - context.push('/host'); //replaces joinOrHostDialog - }, - ), + child: const Text('Host'), + onPressed: () { + context.pop(); //close dialog before going to host + context.goNamed('host'); + }), TextButton( child: const Text('Join'), onPressed: () { + context.pop(); //close dialog before going to next dialog buildEnterPassphraseDialog(context); }, ), @@ -97,15 +94,18 @@ class _LobbySelectorState extends State { return AlertDialog( title: const Text('Enter the passphrase here:'), content: TextField( - controller: myController, + controller: phraseController, decoration: InputDecoration( hintText: 'Enter passphrase here', suffixIcon: IconButton( onPressed: () { joinGameFuture = joinPrivateGame(); joinGameFuture.then((value) { - if (value == null) return; - switchToGame(value); + if (value != null) { + phraseController.clear(); + context.pop(); + switchToGame(value); + } }); }, icon: const Icon(Icons.check), @@ -146,7 +146,7 @@ class _LobbySelectorState extends State { ScaffoldMessenger.of(context).showSnackBar(snackBar); } - context.pushReplacement('/game', extra: chessGameArgs); + context.goNamed('game', extra: chessGameArgs); } Future joinPrivateGame() async { @@ -155,7 +155,7 @@ class _LobbySelectorState extends State { // server expects us to send the passphrase var info = PlayerInfo( - playerID: null, lobbyID: null, passphrase: myController.text); + playerID: null, lobbyID: null, passphrase: phraseController.text); if (kDebugMode) { addr = 'http://localhost:8080/api/joinPrivate'; diff --git a/lib/utils/chess_router.dart b/lib/utils/chess_router.dart index 35892b0..0c3216e 100644 --- a/lib/utils/chess_router.dart +++ b/lib/utils/chess_router.dart @@ -13,6 +13,7 @@ class ChessAppRouter { } final router = GoRouter( + debugLogDiagnostics: true, routes: [ GoRoute( path: '/', @@ -20,26 +21,28 @@ class ChessAppRouter { builder: (context, state) { return const LobbySelector(); }, - ), - GoRoute( - path: '/host', - name: 'host', - builder: (context, state) { - return const HostGameWidget(); - }), - GoRoute( - path: '/game', - name: 'game', - builder: (context, state) { - var args = state.extra as ChessGameArguments; + routes: [ + GoRoute( + path: 'host', + name: 'host', + builder: (context, state) { + return const HostGameWidget(); + }), + GoRoute( + path: 'game', + name: 'game', + builder: (context, state) { + var args = state.extra as ChessGameArguments; - return ChessGame( - lobbyID: args.lobbyID, - playerID: args.playerID, - passphrase: args.passphrase, - ); - }, - ) + return ChessGame( + lobbyID: args.lobbyID, + playerID: args.playerID, + passphrase: args.passphrase, + ); + }, + ) + ], + ), ], ); } diff --git a/pubspec.lock b/pubspec.lock index 0bb45f3..fd46d81 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -140,10 +140,10 @@ packages: dependency: "direct main" description: name: go_router - sha256: c5fa45fa502ee880839e3b2152d987c44abae26d064a2376d4aad434cf0f7b15 + sha256: ca7e4a2249f96773152f1853fa25933ac752495cdd7fdf5dafb9691bd05830fd url: "https://pub.dev" source: hosted - version: "12.1.3" + version: "13.0.0" http: dependency: "direct main" description: @@ -413,6 +413,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.2" + universal_platform: + dependency: "direct main" + description: + name: universal_platform + sha256: d315be0f6641898b280ffa34e2ddb14f3d12b1a37882557869646e0cc363d0cc + url: "https://pub.dev" + source: hosted + version: "1.0.0+1" uuid: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 8ec68e5..6d6b32c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,10 +41,11 @@ dependencies: flutter_bloc: ^8.1.3 quiver: ^3.1.0 web_socket_channel: ^2.2.0 - go_router: ^12.0.0 + go_router: ^13.0.0 http: ^1.0.0 uuid: ^4.0.0 shared_preferences: ^2.2.2 + universal_platform: ^1.0.0+1 dev_dependencies: flutter_test: