mchess-client/lib/pages/prepare_chess_game.dart

44 lines
1.3 KiB
Dart
Raw Normal View History

import 'dart:developer';
import 'package:flutter/material.dart';
2023-06-02 21:28:40 +00:00
import 'package:mchess/api/register.dart';
import 'package:mchess/pages/chess_game.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class PrepareChessGameWidget extends StatelessWidget {
const PrepareChessGameWidget({super.key});
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: registerForRandomGame(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
log('future done ${snapshot.data?.playerID}');
return ChessGame(
playerID: snapshot.data!.playerID,
2023-06-02 21:28:40 +00:00
lobbyID: snapshot.data!.lobbyID,
);
}
return const CircularProgressIndicator();
},
);
}
Future<ResponseFromRegisteringGame> registerForRandomGame() async {
2023-06-02 21:28:40 +00:00
final response = await http.get(
Uri.parse('http://localhost:8080/api/random'),
headers: {"Accept": "application/json"});
if (response.statusCode == 200) {
log(response.body);
return ResponseFromRegisteringGame.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
2023-06-02 21:28:40 +00:00
throw Exception('Failed to register for random game');
}
}
}