continuar a team_page
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:playmaker/controllers/team_controller.dart';
|
||||
import 'dart:async';
|
||||
import '../models/team_model.dart';
|
||||
import '../models/person_model.dart';
|
||||
import '../controllers/stats_controller.dart';
|
||||
import '../widgets/stats_widgets.dart';
|
||||
import '../controllers/team_controllers.dart'; // Para aceder à lista estática de membros
|
||||
|
||||
class TeamStatsPage extends StatelessWidget {
|
||||
final Team team;
|
||||
// Agora este controller já tem o método getMembers
|
||||
final StatsController _controller = StatsController();
|
||||
|
||||
TeamStatsPage({super.key, required this.team});
|
||||
@@ -17,26 +17,16 @@ class TeamStatsPage extends StatelessWidget {
|
||||
backgroundColor: const Color(0xFFF5F7FA),
|
||||
body: Column(
|
||||
children: [
|
||||
// Certifica-te que tens o StatsHeader no ficheiro stats_widgets.dart
|
||||
StatsHeader(team: team),
|
||||
_buildLocalHeader(context),
|
||||
Expanded(
|
||||
child: StreamBuilder<List<Person>>(
|
||||
// AGORA ISTO VAI FUNCIONAR:
|
||||
stream: _controller.getMembers(team.id),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return Center(child: Text('Erro: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
final members = snapshot.data ?? [];
|
||||
if (members.isEmpty) {
|
||||
return const Center(child: Text("Sem membros nesta equipa."));
|
||||
}
|
||||
|
||||
final coaches = members.where((m) => m.type == 'Treinador').toList();
|
||||
final players = members.where((m) => m.type == 'Jogador').toList();
|
||||
|
||||
@@ -45,17 +35,21 @@ class TeamStatsPage extends StatelessWidget {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SummaryCard(), // Confirma se tens este widget
|
||||
_buildLocalSummaryCard(members.length),
|
||||
const SizedBox(height: 30),
|
||||
if (coaches.isNotEmpty) ...[
|
||||
_buildSectionTitle("Treinadores"),
|
||||
...coaches.map((c) => _buildPersonCard(context, c, isCoach: true)),
|
||||
const SizedBox(height: 30),
|
||||
],
|
||||
if (players.isNotEmpty) ...[
|
||||
_buildSectionTitle("Jogadores"),
|
||||
_buildSectionTitle("Jogadores"),
|
||||
if (players.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(top: 20),
|
||||
child: Text("Nenhum jogador adicionado.", style: TextStyle(color: Colors.grey)),
|
||||
)
|
||||
else
|
||||
...players.map((p) => _buildPersonCard(context, p, isCoach: false)),
|
||||
],
|
||||
const SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
@@ -73,37 +67,88 @@ class TeamStatsPage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
// --- WIDGETS QUE ESTAVAM EM STATS_WIDGETS ---
|
||||
|
||||
Widget _buildLocalHeader(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.only(top: 50, left: 20, right: 20, bottom: 20),
|
||||
decoration: const BoxDecoration(
|
||||
color: Color(0xFF2C3E50),
|
||||
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(30), bottomRight: Radius.circular(30)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
CircleAvatar(
|
||||
backgroundColor: Colors.white24,
|
||||
child: Text(team.imageUrl.isNotEmpty && !team.imageUrl.startsWith('http') ? team.imageUrl : "🏀"),
|
||||
),
|
||||
const SizedBox(width: 15),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(team.name, style: const TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
|
||||
Text(team.season, style: const TextStyle(color: Colors.white70, fontSize: 14)),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLocalSummaryCard(int total) {
|
||||
return Card(
|
||||
elevation: 4,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
gradient: LinearGradient(colors: [Colors.blue.shade700, Colors.blue.shade400]),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text("Total de Membros", style: TextStyle(color: Colors.white, fontSize: 16)),
|
||||
Text("$total", style: const TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.bold)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPersonCard(BuildContext context, Person person, {required bool isCoach}) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(top: 12),
|
||||
elevation: 2,
|
||||
color: isCoach ? const Color(0xFFFFF9C4) : Colors.white,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: ListTile(
|
||||
leading: isCoach
|
||||
? const CircleAvatar(radius: 25, backgroundColor: Colors.orange, child: Icon(Icons.person, color: Colors.white))
|
||||
: Container(
|
||||
width: 50, height: 50,
|
||||
alignment: Alignment.center,
|
||||
decoration: BoxDecoration(color: Colors.blue.withOpacity(0.1), borderRadius: BorderRadius.circular(10)),
|
||||
child: Text(person.number, style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 18)),
|
||||
),
|
||||
title: Text(person.name, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 17)),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.edit_outlined, size: 24, color: Colors.blue),
|
||||
onPressed: () => _controller.showEditPersonDialog(context, team.id, person),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline, size: 24, color: Colors.red),
|
||||
onPressed: () => _confirmDelete(context, person),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ListTile(
|
||||
leading: isCoach
|
||||
? const CircleAvatar(backgroundColor: Colors.orange, child: Icon(Icons.person, color: Colors.white))
|
||||
: Container(
|
||||
width: 45, height: 45,
|
||||
alignment: Alignment.center,
|
||||
decoration: BoxDecoration(color: Colors.blue.withOpacity(0.1), borderRadius: BorderRadius.circular(10)),
|
||||
child: Text(person.number, style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 16)),
|
||||
),
|
||||
title: Text(person.name, style: const TextStyle(fontWeight: FontWeight.bold)),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.edit_outlined, color: Colors.blue),
|
||||
onPressed: () => _controller.showEditPersonDialog(context, team.id, person),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline, color: Colors.red),
|
||||
onPressed: () => _confirmDelete(context, person),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -114,7 +159,7 @@ class TeamStatsPage extends StatelessWidget {
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text("Eliminar?"),
|
||||
content: Text("Queres mesmo remover ${person.name}?"),
|
||||
content: Text("Remover ${person.name}?"),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text("Cancelar")),
|
||||
TextButton(
|
||||
@@ -134,9 +179,92 @@ class TeamStatsPage extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Color(0xFF2C3E50))),
|
||||
const SizedBox(height: 10),
|
||||
const Divider(),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- CONTROLLER LOCAL (SEM SUPABASE) ---
|
||||
|
||||
class StatsController {
|
||||
final _streamController = StreamController<List<Person>>.broadcast();
|
||||
|
||||
Stream<List<Person>> getMembers(String teamId) {
|
||||
_emitMembers(teamId);
|
||||
return _streamController.stream;
|
||||
}
|
||||
|
||||
void _emitMembers(String teamId) {
|
||||
final list = TeamController.members
|
||||
.where((m) => m['team_id'] == teamId)
|
||||
.map((json) => Person.fromMap(json))
|
||||
.toList();
|
||||
_streamController.add(list);
|
||||
}
|
||||
|
||||
void deletePerson(String teamId, String personId) {
|
||||
TeamController.members.removeWhere((m) => m['id'] == personId);
|
||||
_emitMembers(teamId);
|
||||
}
|
||||
|
||||
void showAddPersonDialog(BuildContext context, String teamId) {
|
||||
_showForm(context, teamId: teamId);
|
||||
}
|
||||
|
||||
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
||||
_showForm(context, teamId: teamId, person: person);
|
||||
}
|
||||
|
||||
void _showForm(BuildContext context, {required String teamId, Person? person}) {
|
||||
final isEdit = person != null;
|
||||
final nameCtrl = TextEditingController(text: person?.name ?? '');
|
||||
final numCtrl = TextEditingController(text: person?.number ?? '');
|
||||
String type = person?.type ?? 'Jogador';
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => StatefulBuilder(
|
||||
builder: (ctx, setState) => AlertDialog(
|
||||
title: Text(isEdit ? "Editar" : "Adicionar"),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(controller: nameCtrl, decoration: const InputDecoration(labelText: "Nome")),
|
||||
DropdownButton<String>(
|
||||
value: type,
|
||||
isExpanded: true,
|
||||
items: ["Jogador", "Treinador"].map((e) => DropdownMenuItem(value: e, child: Text(e))).toList(),
|
||||
onChanged: (v) => setState(() => type = v!),
|
||||
),
|
||||
if (type == "Jogador")
|
||||
TextField(controller: numCtrl, decoration: const InputDecoration(labelText: "Número"), keyboardType: TextInputType.number),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(ctx), child: const Text("Sair")),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
if (isEdit) {
|
||||
final idx = TeamController.members.indexWhere((m) => m['id'] == person.id);
|
||||
TeamController.members[idx] = {'id': person.id, 'team_id': teamId, 'name': nameCtrl.text, 'type': type, 'number': numCtrl.text};
|
||||
} else {
|
||||
TeamController.members.add({
|
||||
'id': DateTime.now().toString(),
|
||||
'team_id': teamId,
|
||||
'name': nameCtrl.text,
|
||||
'type': type,
|
||||
'number': numCtrl.text
|
||||
});
|
||||
}
|
||||
_emitMembers(teamId);
|
||||
Navigator.pop(ctx);
|
||||
},
|
||||
child: const Text("Guardar"),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user