103 lines
3.6 KiB
Dart
103 lines
3.6 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';
|
|
|
|
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) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFF5F7FA),
|
|
appBar: AppBar(
|
|
title: const Text("Jogos", style: TextStyle(fontWeight: FontWeight.bold)),
|
|
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}"));
|
|
}
|
|
|
|
if (!gameSnapshot.hasData || gameSnapshot.data!.isEmpty) {
|
|
return const Center(child: Text("Nenhum jogo registado."));
|
|
}
|
|
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
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'];
|
|
}
|
|
}
|
|
|
|
// Agora já passamos as imagens para o cartão!
|
|
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, // <-- IMAGEM DA TUA EQUIPA
|
|
opponentTeamLogo: oppLogo, // <-- IMAGEM DA EQUIPA ADVERSÁRIA
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
backgroundColor: const Color(0xFFE74C3C),
|
|
child: const Icon(Icons.add, color: Colors.white),
|
|
onPressed: () => _showCreateDialog(context),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showCreateDialog(BuildContext context) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => CreateGameDialogManual(
|
|
teamController: teamController,
|
|
gameController: gameController,
|
|
),
|
|
);
|
|
}
|
|
} |