From ba478fedca47ee521c2a21e4aa6d35268d16523f Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 5 Feb 2024 10:35:28 +0100 Subject: [PATCH] Fix snackbar in host/join dialog --- lib/pages/lobby_selector.dart | 102 +++++++++++++++++++++------------- lib/utils/config.dart | 8 +-- 2 files changed, 66 insertions(+), 44 deletions(-) diff --git a/lib/pages/lobby_selector.dart b/lib/pages/lobby_selector.dart index 67f38aa..cc6e962 100644 --- a/lib/pages/lobby_selector.dart +++ b/lib/pages/lobby_selector.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'dart:developer'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; @@ -58,26 +59,27 @@ class _LobbySelectorState extends State { Future buildJoinOrHostDialog(BuildContext context) { return showDialog( context: context, - builder: (BuildContext context) { + builder: (BuildContext builderContext) { return Scaffold( body: AlertDialog( title: const Text('Host or join?'), actions: [ TextButton( child: const Text('Cancel'), - onPressed: () => context.pop(), + onPressed: () => builderContext.pop(), ), TextButton( child: const Text('Host'), onPressed: () { - context.pop(); //close dialog before going to host - context.goNamed('host'); + builderContext.pop(); //close dialog before going to host + builderContext.goNamed('host'); }), TextButton( child: const Text('Join'), onPressed: () { - context.pop(); //close dialog before going to next dialog - buildEnterPassphraseDialog(context); + builderContext + .pop(); //close dialog before going to next dialog + buildEnterPassphraseDialog(builderContext); }, ), ], @@ -91,34 +93,41 @@ class _LobbySelectorState extends State { return showDialog( context: context, builder: (BuildContext context) { - return AlertDialog( - title: const Text('Enter the passphrase here:'), - content: TextField( - controller: phraseController, - decoration: InputDecoration( - hintText: 'Enter passphrase here', - suffixIcon: IconButton( - onPressed: () { - joinGameFuture = joinPrivateGame(); - joinGameFuture.then((value) { - if (value != null) { - phraseController.clear(); - context.pop(); - switchToGame(value); - } - }); - }, - icon: const Icon(Icons.check), - )), - ), - actions: [ - TextButton( - child: const Text('Cancel'), - onPressed: () { - context.pop(); - }, - ), - ], + return ScaffoldMessenger( + child: Builder(builder: (builderContext) { + return Scaffold( + backgroundColor: Colors.transparent, + body: AlertDialog( + title: const Text('Enter the passphrase here:'), + content: TextField( + controller: phraseController, + decoration: InputDecoration( + hintText: 'Enter passphrase here', + suffixIcon: IconButton( + onPressed: () { + joinGameFuture = joinPrivateGame(builderContext); + joinGameFuture.then((value) { + if (value != null) { + phraseController.clear(); + builderContext.pop(); + switchToGame(value); + } + }); + }, + icon: const Icon(Icons.check), + )), + ), + actions: [ + TextButton( + child: const Text('Cancel'), + onPressed: () { + builderContext.pop(); + }, + ), + ], + ), + ); + }), ); }, ); @@ -144,12 +153,14 @@ class _LobbySelectorState extends State { ); ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar(snackBar); + + return; } context.goNamed('game', extra: chessGameArgs); } - Future joinPrivateGame() async { + Future joinPrivateGame(BuildContext context) async { http.Response response; // server expects us to send the passphrase @@ -168,14 +179,25 @@ class _LobbySelectorState extends State { backgroundColor: Colors.amberAccent, content: Text("mChess server is not responding. Try again or give up"), ); - if (mounted) { - ScaffoldMessenger.of(context).clearSnackBars(); - ScaffoldMessenger.of(context).showSnackBar(snackBar); - } + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(snackBar); return null; } - if (response.statusCode == 200) { + if (response.statusCode == HttpStatus.notFound) { + const snackBar = SnackBar( + backgroundColor: Colors.amberAccent, + content: Text("Passphrase could not be found."), + ); + + if (!context.mounted) return null; + + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(snackBar); + return null; + } + + if (response.statusCode == HttpStatus.ok) { var info = PlayerInfo.fromJson(jsonDecode(response.body)); log('Player info received from server: '); log('lobbyID: ${info.lobbyID}'); diff --git a/lib/utils/config.dart b/lib/utils/config.dart index ca484a0..863dedb 100644 --- a/lib/utils/config.dart +++ b/lib/utils/config.dart @@ -1,12 +1,12 @@ const prodURL = 'chess.sw-gross.de:9999'; const debugURL = 'localhost:8080'; -const dbgUrl = false; +const useDbgUrl = false; String getHostURL() { var prot = 'https'; var domain = prodURL; - if (dbgUrl) { + if (useDbgUrl) { prot = 'http'; domain = debugURL; } @@ -16,7 +16,7 @@ String getHostURL() { String getJoinURL() { var prot = 'https'; var domain = prodURL; - if (dbgUrl) { + if (useDbgUrl) { prot = 'http'; domain = debugURL; } @@ -26,7 +26,7 @@ String getJoinURL() { String getWebsocketURL() { var prot = 'wss'; var domain = prodURL; - if (dbgUrl) { + if (useDbgUrl) { prot = 'ws'; domain = debugURL; }