98 lines
2.5 KiB
Dart
98 lines
2.5 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../../globals.dart';
|
|
|
|
class LeaderboardPage extends StatefulWidget {
|
|
@override
|
|
_LeaderboardPageState createState() => _LeaderboardPageState();
|
|
}
|
|
|
|
class _LeaderboardPageState extends State<LeaderboardPage> {
|
|
List<dynamic> _leaderboard = [];
|
|
bool _isLoading = true;
|
|
|
|
final String _leaderboardApi = '$apiurl/leaderboards';
|
|
|
|
Future<void> _fetchLeaderboard() async {
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final response = await http.get(Uri.parse(_leaderboardApi));
|
|
|
|
if (response.statusCode == 200) {
|
|
List<dynamic> data = json.decode(response.body);
|
|
setState(() {
|
|
_leaderboard = data;
|
|
_isLoading = false;
|
|
});
|
|
} else {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
_showError('Failed to load data');
|
|
}
|
|
} catch (e) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
_showError('Error: $e');
|
|
}
|
|
}
|
|
|
|
void _showError(String message) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text('Error'),
|
|
content: Text(message),
|
|
actions: [
|
|
TextButton(
|
|
child: Text('OK'),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchLeaderboard();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: _isLoading
|
|
? Center(child: CircularProgressIndicator())
|
|
: RefreshIndicator(
|
|
onRefresh: _fetchLeaderboard,
|
|
child: ListView.builder(
|
|
itemCount: _leaderboard.length,
|
|
itemBuilder: (context, index) {
|
|
var player = _leaderboard[index];
|
|
return Card(
|
|
margin: EdgeInsets.all(8),
|
|
child: ListTile(
|
|
contentPadding: EdgeInsets.all(10),
|
|
leading: CircleAvatar(
|
|
child: Text(player['player_name'][0].toUpperCase()),
|
|
),
|
|
title: Text(player['player_name']),
|
|
subtitle: Text('Elo Rating: ${player['elo_rating']}'),
|
|
trailing: Text('Friend Code: ${player['friend_code']}'),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|