corrrigi team_stats_page
This commit is contained in:
@@ -5,71 +5,52 @@ import '../models/person_model.dart';
|
||||
class StatsController {
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
// --- 1. LER DADOS (STREAM) ---
|
||||
// --- 1. LER MEMBROS (STREAM EM TEMPO REAL) ---
|
||||
Stream<List<Person>> getMembers(String teamId) {
|
||||
return _supabase
|
||||
.from('members')
|
||||
.from('members') // Nome da tua tabela no Supabase
|
||||
.stream(primaryKey: ['id'])
|
||||
.eq('team_id', teamId)
|
||||
.order('name', ascending: true) // Ordena por nome
|
||||
.order('name', ascending: true)
|
||||
.map((data) => data.map((json) => Person.fromMap(json)).toList());
|
||||
}
|
||||
|
||||
// --- 2. AÇÕES DE BASE DE DADOS ---
|
||||
|
||||
// Adicionar
|
||||
Future<void> _addPersonToSupabase(String teamId, String name, String type, String number) async {
|
||||
await _supabase.from('members').insert({
|
||||
'team_id': teamId,
|
||||
'name': name,
|
||||
'type': type,
|
||||
'number': number,
|
||||
});
|
||||
}
|
||||
|
||||
// Editar
|
||||
Future<void> _updatePersonInSupabase(String personId, String name, String type, String number) async {
|
||||
await _supabase.from('members').update({
|
||||
'name': name,
|
||||
'type': type,
|
||||
'number': number,
|
||||
}).eq('id', personId);
|
||||
}
|
||||
|
||||
// Apagar
|
||||
// --- 2. APAGAR ---
|
||||
Future<void> deletePerson(String teamId, String personId) async {
|
||||
try {
|
||||
await _supabase.from('members').delete().eq('id', personId);
|
||||
} catch (e) {
|
||||
debugPrint("Erro ao apagar: $e");
|
||||
debugPrint("Erro ao eliminar: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// --- 3. DIÁLOGOS (UI) ---
|
||||
// --- 3. DIÁLOGOS DE ADICIONAR / EDITAR ---
|
||||
|
||||
// Mostrar Diálogo de Adicionar
|
||||
// Abrir diálogo para criar novo
|
||||
void showAddPersonDialog(BuildContext context, String teamId) {
|
||||
_showPersonDialog(context, teamId: teamId);
|
||||
_showPersonForm(context, teamId: teamId);
|
||||
}
|
||||
|
||||
// Mostrar Diálogo de Editar
|
||||
// Abrir diálogo para editar existente
|
||||
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
||||
_showPersonDialog(context, teamId: teamId, person: person);
|
||||
_showPersonForm(context, teamId: teamId, person: person);
|
||||
}
|
||||
|
||||
// Função Genérica para o Diálogo (Serve para criar e editar)
|
||||
void _showPersonDialog(BuildContext context, {required String teamId, Person? person}) {
|
||||
// Lógica interna do formulário
|
||||
void _showPersonForm(BuildContext context, {required String teamId, Person? person}) {
|
||||
final isEditing = person != null;
|
||||
|
||||
// Controladores de texto
|
||||
final nameController = TextEditingController(text: person?.name ?? '');
|
||||
final numberController = TextEditingController(text: person?.number ?? '');
|
||||
|
||||
// Valor inicial do dropdown ('Jogador' por defeito)
|
||||
// Variável para o Dropdown (Valor inicial)
|
||||
String selectedType = person?.type ?? 'Jogador';
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
// Usamos StatefulBuilder para atualizar o Dropdown dentro do Dialog
|
||||
// StatefulBuilder serve para atualizar o Dropdown DENTRO do diálogo
|
||||
return StatefulBuilder(
|
||||
builder: (context, setState) {
|
||||
return AlertDialog(
|
||||
@@ -85,7 +66,7 @@ class StatsController {
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// Tipo (Jogador/Treinador)
|
||||
// Tipo (Jogador vs Treinador)
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedType,
|
||||
decoration: const InputDecoration(labelText: 'Função'),
|
||||
@@ -101,7 +82,7 @@ class StatsController {
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// Número (Só aparece se for Jogador)
|
||||
// Número (Só mostramos se for Jogador)
|
||||
if (selectedType == 'Jogador')
|
||||
TextField(
|
||||
controller: numberController,
|
||||
@@ -118,15 +99,26 @@ class StatsController {
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
|
||||
onPressed: () async {
|
||||
if (nameController.text.isEmpty) return;
|
||||
if (nameController.text.trim().isEmpty) return;
|
||||
|
||||
final name = nameController.text.trim();
|
||||
final number = numberController.text.trim();
|
||||
|
||||
if (isEditing) {
|
||||
await _updatePersonInSupabase(person!.id, name, selectedType, number);
|
||||
// ATUALIZAR
|
||||
await _supabase.from('members').update({
|
||||
'name': name,
|
||||
'type': selectedType,
|
||||
'number': number,
|
||||
}).eq('id', person!.id);
|
||||
} else {
|
||||
await _addPersonToSupabase(teamId, name, selectedType, number);
|
||||
// CRIAR NOVO
|
||||
await _supabase.from('members').insert({
|
||||
'team_id': teamId,
|
||||
'name': name,
|
||||
'type': selectedType,
|
||||
'number': number,
|
||||
});
|
||||
}
|
||||
|
||||
if (context.mounted) Navigator.pop(context);
|
||||
|
||||
@@ -13,7 +13,7 @@ class Person {
|
||||
required this.number,
|
||||
});
|
||||
|
||||
// Converter do Supabase (Map) para o Objeto
|
||||
// Converte o JSON do Supabase para o objeto Person
|
||||
factory Person.fromMap(Map<String, dynamic> map) {
|
||||
return Person(
|
||||
id: map['id'] ?? '',
|
||||
|
||||
@@ -6,6 +6,7 @@ import '../widgets/stats_widgets.dart';
|
||||
|
||||
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});
|
||||
@@ -16,16 +17,26 @@ 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),
|
||||
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();
|
||||
|
||||
@@ -34,13 +45,17 @@ class TeamStatsPage extends StatelessWidget {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SummaryCard(),
|
||||
const SummaryCard(), // Confirma se tens este widget
|
||||
const SizedBox(height: 30),
|
||||
_buildSectionTitle("Treinadores"),
|
||||
...coaches.map((c) => _buildPersonCard(context, c, isCoach: true)),
|
||||
const SizedBox(height: 30),
|
||||
_buildSectionTitle("Jogadores"),
|
||||
...players.map((p) => _buildPersonCard(context, p, isCoach: false)),
|
||||
if (coaches.isNotEmpty) ...[
|
||||
_buildSectionTitle("Treinadores"),
|
||||
...coaches.map((c) => _buildPersonCard(context, c, isCoach: true)),
|
||||
const SizedBox(height: 30),
|
||||
],
|
||||
if (players.isNotEmpty) ...[
|
||||
_buildSectionTitle("Jogadores"),
|
||||
...players.map((p) => _buildPersonCard(context, p, isCoach: false)),
|
||||
],
|
||||
const SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
@@ -58,7 +73,6 @@ class TeamStatsPage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
// CORREÇÃO: Adicionado BuildContext context como argumento
|
||||
Widget _buildPersonCard(BuildContext context, Person person, {required bool isCoach}) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(top: 12),
|
||||
@@ -126,6 +140,3 @@ class TeamStatsPage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class StatsController {
|
||||
}
|
||||
Reference in New Issue
Block a user