Merge pull request 'Fix routing again' (#4) from fix-routing-again into master

Reviewed-on: #4
This commit is contained in:
marco 2023-12-27 15:49:04 +01:00
commit 2fe77063d9
7 changed files with 71 additions and 56 deletions

View File

@ -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());
}

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_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<ChessGame> {
@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<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();
});
connectToWebsocket(registerResponse);
super.initState();
}
@ -70,7 +69,7 @@ class _HostGameWidgetState extends State<HostGameWidget> {
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<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), () {
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;
}

View File

@ -19,7 +19,7 @@ class LobbySelector extends StatefulWidget {
class _LobbySelectorState extends State<LobbySelector> {
final buttonStyle = const ButtonStyle();
final myController = TextEditingController();
final phraseController = TextEditingController();
late Future<PlayerInfo?> joinGameFuture;
@override
@ -37,9 +37,7 @@ class _LobbySelectorState extends State<LobbySelector> {
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<LobbySelector> {
actions: <Widget>[
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<LobbySelector> {
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<LobbySelector> {
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
context.pushReplacement('/game', extra: chessGameArgs);
context.goNamed('game', extra: chessGameArgs);
}
Future<PlayerInfo?> joinPrivateGame() async {
@ -155,7 +155,7 @@ class _LobbySelectorState extends State<LobbySelector> {
// 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';

View File

@ -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,
);
},
)
],
),
],
);
}

View File

@ -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:

View File

@ -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: