2024-12-21 22:36:41 +01:00
import ' dart:convert ' ;
2024-12-21 19:41:00 +01:00
import ' package:flutter/material.dart ' ;
2024-12-21 22:36:41 +01:00
import ' package:http/http.dart ' as http ;
import ' package:shared_preferences/shared_preferences.dart ' ;
2024-12-23 21:48:34 +01:00
import ' ../../globals.dart ' ;
2024-12-21 19:41:00 +01:00
2024-12-21 22:36:41 +01:00
class CreateMatchPage extends StatefulWidget {
@ override
_CreateMatchPageState createState ( ) = > _CreateMatchPageState ( ) ;
}
class _CreateMatchPageState extends State < CreateMatchPage > {
String ? _matchId ;
bool _isLoading = false ;
2024-12-23 21:48:34 +01:00
final String _createMatchApiUrl =
' $ apiurl /creatematch ' ; // Replace with your API endpoint
2024-12-21 22:36:41 +01:00
// Method to create a match
Future < void > _createMatch ( ) async {
setState ( ( ) {
_isLoading = true ;
} ) ;
2024-12-21 19:41:00 +01:00
2024-12-21 22:36:41 +01:00
final prefs = await SharedPreferences . getInstance ( ) ;
final String ? token = prefs . getString ( ' token ' ) ;
if ( token = = null ) {
_showToast ( ' No token found. Please login again. ' ) ;
setState ( ( ) {
_isLoading = false ;
} ) ;
return ;
}
try {
final response = await http . post (
Uri . parse ( _createMatchApiUrl ) ,
headers: { ' Content-Type ' : ' application/json ' } ,
body: json . encode ( { ' token ' : token } ) ,
) ;
if ( response . statusCode = = 200 ) {
final data = json . decode ( response . body ) ;
setState ( ( ) {
_matchId = data [ ' match_id ' ] . toString ( ) ;
} ) ;
_showToast ( ' Match created successfully! ' ) ;
} else {
_showToast ( ' Failed to create match. ' ) ;
}
} catch ( e ) {
_showToast ( ' Error: $ e ' ) ;
} finally {
setState ( ( ) {
_isLoading = false ;
} ) ;
}
}
// Show a Toast message (SnackBar in Flutter)
void _showToast ( String message ) {
ScaffoldMessenger . of ( context ) . showSnackBar (
SnackBar ( content: Text ( message ) ) ,
) ;
}
2024-12-21 19:41:00 +01:00
@ override
Widget build ( BuildContext context ) {
return Scaffold (
2024-12-21 22:36:41 +01:00
appBar: AppBar (
title: Text ( ' Create Match ' ) ,
) ,
2024-12-21 19:41:00 +01:00
body: Center (
child: Padding (
padding: const EdgeInsets . all ( 16.0 ) ,
child: Column (
mainAxisAlignment: MainAxisAlignment . center ,
crossAxisAlignment: CrossAxisAlignment . center ,
children: [
Icon (
2024-12-21 22:36:41 +01:00
Icons . sports_tennis ,
2024-12-21 19:41:00 +01:00
size: 80 ,
2024-12-21 22:36:41 +01:00
color: Colors . blue ,
2024-12-21 19:41:00 +01:00
) ,
2024-12-21 22:36:41 +01:00
SizedBox ( height: 16 ) ,
2024-12-21 19:41:00 +01:00
Text (
2024-12-21 22:36:41 +01:00
' Create a Match ' ,
style: TextStyle ( fontSize: 24 , fontWeight: FontWeight . bold ) ,
2024-12-21 19:41:00 +01:00
textAlign: TextAlign . center ,
) ,
SizedBox ( height: 16 ) ,
2024-12-25 12:08:09 +01:00
Text (
' Due to current limitations in how we handle matchmaking, only the joining player can control the match. This is only a temporary solution to a problem we are actively fixing. ' ,
style: TextStyle ( fontSize: 14 ) ,
textAlign: TextAlign . center ,
) ,
SizedBox ( height: 16 ) ,
2024-12-21 22:36:41 +01:00
_isLoading
? CircularProgressIndicator ( ) // Show loading spinner
: ElevatedButton (
2024-12-23 21:48:34 +01:00
onPressed: _createMatch ,
child: Text ( ' Create Match ' ) ,
) ,
2024-12-21 22:36:41 +01:00
SizedBox ( height: 16 ) ,
if ( _matchId ! = null )
Text (
' Your Match ID: $ _matchId ' ,
style: TextStyle ( fontSize: 18 , color: Colors . green ) ,
textAlign: TextAlign . center ,
) ,
2024-12-21 19:41:00 +01:00
] ,
) ,
) ,
) ,
) ;
}
}