Files
PlayMaker/lib/pages/gamePage.dart
2026-03-10 17:15:10 +00:00

110 lines
4.0 KiB
Dart

import 'package:flutter/material.dart';
import '../controllers/game_controller.dart';
import '../controllers/team_controller.dart';
import '../models/game_model.dart';
import '../widgets/game_widgets.dart';
import 'dart:math' as math; // <-- IMPORTANTE: Para o cálculo da escala
class GamePage extends StatefulWidget {
const GamePage({super.key});
@override
State<GamePage> createState() => _GamePageState();
}
class _GamePageState extends State<GamePage> {
final GameController gameController = GameController();
final TeamController teamController = TeamController();
@override
Widget build(BuildContext context) {
// 👇 CÁLCULO DA ESCALA (sf) PARA SE ADAPTAR A QUALQUER ECRÃ 👇
final double wScreen = MediaQuery.of(context).size.width;
final double hScreen = MediaQuery.of(context).size.height;
final double sf = math.min(wScreen, hScreen) / 400;
return Scaffold(
backgroundColor: const Color(0xFFF5F7FA),
appBar: AppBar(
title: Text("Jogos", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20 * sf)),
backgroundColor: Colors.white,
elevation: 0,
),
// 1º STREAM: Lemos as equipas para ter as imagens
body: StreamBuilder<List<Map<String, dynamic>>>(
stream: teamController.teamsStream,
builder: (context, teamSnapshot) {
final List<Map<String, dynamic>> teamsList = teamSnapshot.data ?? [];
// 2º STREAM: Lemos os jogos
return StreamBuilder<List<Game>>(
stream: gameController.gamesStream,
builder: (context, gameSnapshot) {
if (gameSnapshot.connectionState == ConnectionState.waiting && teamsList.isEmpty) {
return const Center(child: CircularProgressIndicator());
}
if (gameSnapshot.hasError) {
return Center(child: Text("Erro: ${gameSnapshot.error}", style: TextStyle(fontSize: 14 * sf)));
}
if (!gameSnapshot.hasData || gameSnapshot.data!.isEmpty) {
return Center(child: Text("Nenhum jogo registado.", style: TextStyle(fontSize: 16 * sf)));
}
return ListView.builder(
padding: EdgeInsets.all(16 * sf),
itemCount: gameSnapshot.data!.length,
itemBuilder: (context, index) {
final game = gameSnapshot.data![index];
// --- LÓGICA PARA ENCONTRAR A IMAGEM PELO NOME ---
String? myLogo;
String? oppLogo;
for (var team in teamsList) {
if (team['name'] == game.myTeam) {
myLogo = team['image_url'];
}
if (team['name'] == game.opponentTeam) {
oppLogo = team['image_url'];
}
}
return GameResultCard(
gameId: game.id,
myTeam: game.myTeam,
opponentTeam: game.opponentTeam,
myScore: game.myScore,
opponentScore: game.opponentScore,
status: game.status,
season: game.season,
myTeamLogo: myLogo,
opponentTeamLogo: oppLogo,
sf: sf, // <-- Passamos a escala para o Cartão
);
},
);
},
);
},
),
floatingActionButton: FloatingActionButton(
backgroundColor: const Color(0xFFE74C3C),
child: Icon(Icons.add, color: Colors.white, size: 24 * sf),
onPressed: () => _showCreateDialog(context, sf),
),
);
}
void _showCreateDialog(BuildContext context, double sf) {
showDialog(
context: context,
builder: (context) => CreateGameDialogManual(
teamController: teamController,
gameController: gameController,
sf: sf, // <-- Passamos a escala para o Pop-up
),
);
}
}