Fix routing again #4

Merged
marco merged 1 commits from fix-routing-again into master 2023-12-27 14:49:04 +00:00
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) {
phraseController.clear();
context.pop();
switchToGame(value); 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';

View File

@ -13,6 +13,7 @@ class ChessAppRouter {
} }
final router = GoRouter( final router = GoRouter(
debugLogDiagnostics: true,
routes: [ routes: [
GoRoute( GoRoute(
path: '/', path: '/',
@ -20,15 +21,15 @@ class ChessAppRouter {
builder: (context, state) { builder: (context, state) {
return const LobbySelector(); return const LobbySelector();
}, },
), routes: [
GoRoute( GoRoute(
path: '/host', path: 'host',
name: 'host', name: 'host',
builder: (context, state) { builder: (context, state) {
return const HostGameWidget(); return const HostGameWidget();
}), }),
GoRoute( GoRoute(
path: '/game', path: 'game',
name: 'game', name: 'game',
builder: (context, state) { builder: (context, state) {
var args = state.extra as ChessGameArguments; var args = state.extra as ChessGameArguments;
@ -41,5 +42,7 @@ class ChessAppRouter {
}, },
) )
], ],
),
],
); );
} }

View File

@ -140,10 +140,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: go_router name: go_router
sha256: c5fa45fa502ee880839e3b2152d987c44abae26d064a2376d4aad434cf0f7b15 sha256: ca7e4a2249f96773152f1853fa25933ac752495cdd7fdf5dafb9691bd05830fd
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "12.1.3" version: "13.0.0"
http: http:
dependency: "direct main" dependency: "direct main"
description: description:
@ -413,6 +413,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.2" 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: uuid:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -41,10 +41,11 @@ dependencies:
flutter_bloc: ^8.1.3 flutter_bloc: ^8.1.3
quiver: ^3.1.0 quiver: ^3.1.0
web_socket_channel: ^2.2.0 web_socket_channel: ^2.2.0
go_router: ^12.0.0 go_router: ^13.0.0
http: ^1.0.0 http: ^1.0.0
uuid: ^4.0.0 uuid: ^4.0.0
shared_preferences: ^2.2.2 shared_preferences: ^2.2.2
universal_platform: ^1.0.0+1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: