import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; class LeaderboardPage extends StatefulWidget { @override _LeaderboardPageState createState() => _LeaderboardPageState(); } class _LeaderboardPageState extends State { List _leaderboard = []; bool _isLoading = true; final String _apiUrl = 'http://api.dthpp.mercurio.moe/leaderboards'; Future _fetchLeaderboard() async { setState(() { _isLoading = true; }); try { final response = await http.get(Uri.parse(_apiUrl)); if (response.statusCode == 200) { List 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']}'), ), ); }, ), ), ); } }