2023-05-28 12:54:46 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2023-05-28 15:44:22 +00:00
|
|
|
import 'package:mchess/models/models.dart';
|
2023-05-28 12:54:46 +00:00
|
|
|
import 'package:mchess/pages/chess_game.dart';
|
2023-05-28 15:44:22 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'dart:convert';
|
2023-05-28 12:54:46 +00:00
|
|
|
|
|
|
|
class PrepareChessGameWidget extends StatelessWidget {
|
2023-05-28 15:44:22 +00:00
|
|
|
const PrepareChessGameWidget({super.key});
|
2023-05-28 12:54:46 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return FutureBuilder(
|
2023-05-28 15:44:22 +00:00
|
|
|
future: registerForRandomGame(),
|
2023-05-28 12:54:46 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
2023-05-28 15:44:22 +00:00
|
|
|
log('future done ${snapshot.data?.playerID}');
|
|
|
|
return ChessGame(
|
|
|
|
playerID: snapshot.data!.playerID,
|
|
|
|
);
|
2023-05-28 12:54:46 +00:00
|
|
|
}
|
|
|
|
return const CircularProgressIndicator();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-05-28 15:44:22 +00:00
|
|
|
|
|
|
|
Future<ResponseFromRegisteringGame> registerForRandomGame() async {
|
|
|
|
final response =
|
|
|
|
await http.get(Uri.parse('http://localhost:8080/api/random'));
|
|
|
|
|
|
|
|
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.
|
|
|
|
throw Exception('Failed to load album');
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 12:54:46 +00:00
|
|
|
}
|