Merge pull request 'Fix snackbar in host/join dialog' (#9) from fix-dialog-snackbar into master

Reviewed-on: #9
This commit is contained in:
marco 2024-02-05 10:48:30 +01:00
commit fd51e582af
2 changed files with 59 additions and 38 deletions

View File

@ -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';
@ -91,34 +92,41 @@ class _LobbySelectorState extends State<LobbySelector> {
return showDialog<void>(
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: <Widget>[
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: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
builderContext.pop();
},
),
],
),
);
}),
);
},
);
@ -144,12 +152,14 @@ class _LobbySelectorState extends State<LobbySelector> {
);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
return;
}
context.goNamed('game', extra: chessGameArgs);
}
Future<PlayerInfo?> joinPrivateGame() async {
Future<PlayerInfo?> joinPrivateGame(BuildContext context) async {
http.Response response;
// server expects us to send the passphrase
@ -168,14 +178,25 @@ class _LobbySelectorState extends State<LobbySelector> {
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}');

View File

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