stats page pagina melhrar
This commit is contained in:
@@ -1,146 +1,114 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/person_model.dart';
|
||||
|
||||
class StatsController {
|
||||
final FirebaseFirestore _db = FirebaseFirestore.instance;
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
// --- LÓGICA DE FIREBASE ---
|
||||
// --- 1. LER DADOS (STREAM) ---
|
||||
Stream<List<Person>> getMembers(String teamId) {
|
||||
return _supabase
|
||||
.from('members')
|
||||
.stream(primaryKey: ['id'])
|
||||
.eq('team_id', teamId)
|
||||
.order('name', ascending: true) // Ordena por nome
|
||||
.map((data) => data.map((json) => Person.fromMap(json)).toList());
|
||||
}
|
||||
|
||||
// GRAVAR: Cria o personagem numa sub-coleção dentro da equipa
|
||||
Future<void> addPerson(String teamId, String name, String type, String number) async {
|
||||
await _db.collection('teams').doc(teamId).collection('members').add({
|
||||
// --- 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,
|
||||
'createdAt': FieldValue.serverTimestamp(),
|
||||
});
|
||||
}
|
||||
|
||||
// LER: Vai buscar todos os membros da equipa em tempo real
|
||||
Stream<List<Person>> getMembers(String teamId) {
|
||||
return _db
|
||||
.collection('teams')
|
||||
.doc(teamId)
|
||||
.collection('members')
|
||||
.orderBy('createdAt', descending: false) // Organiza por ordem de criação
|
||||
.snapshots()
|
||||
.map((snapshot) => snapshot.docs
|
||||
.map((doc) => Person.fromFirestore(doc.data(), doc.id))
|
||||
.toList());
|
||||
// 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);
|
||||
}
|
||||
// --- Adiciona estas funções dentro da classe StatsController ---
|
||||
|
||||
// ELIMINAR: Remove o documento da sub-coleção
|
||||
Future<void> deletePerson(String teamId, String personId) async {
|
||||
await _db
|
||||
.collection('teams')
|
||||
.doc(teamId)
|
||||
.collection('members')
|
||||
.doc(personId)
|
||||
.delete();
|
||||
}
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
||||
// EDITAR (LOGICA): Abre o popup já preenchido com os dados atuais
|
||||
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
||||
final nameController = TextEditingController(text: person.name);
|
||||
final numberController = TextEditingController(text: person.number);
|
||||
String selectedType = person.type;
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => StatefulBuilder(
|
||||
builder: (context, setPopupState) => AlertDialog(
|
||||
title: const Text("Editar Personagem"),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedType,
|
||||
items: ['Jogador', 'Treinador'].map((t) => DropdownMenuItem(value: t, child: Text(t))).toList(),
|
||||
onChanged: (val) => setPopupState(() => selectedType = val!),
|
||||
decoration: const InputDecoration(labelText: 'Tipo'),
|
||||
),
|
||||
TextField(controller: nameController, decoration: const InputDecoration(labelText: 'Nome')),
|
||||
if (selectedType == 'Jogador')
|
||||
TextField(controller: numberController, decoration: const InputDecoration(labelText: 'Número')),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text("Cancelar")),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
await _db.collection('teams').doc(teamId).collection('members').doc(person.id).update({
|
||||
'name': nameController.text,
|
||||
'type': selectedType,
|
||||
'number': selectedType == 'Jogador' ? numberController.text : '',
|
||||
});
|
||||
if (context.mounted) Navigator.pop(context);
|
||||
},
|
||||
child: const Text("Guardar Alterações"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// --- LÓGICA DE INTERFACE (POPUP) ---
|
||||
// --- 3. DIÁLOGOS (UI) ---
|
||||
|
||||
// Mostrar Diálogo de Adicionar
|
||||
void showAddPersonDialog(BuildContext context, String teamId) {
|
||||
String selectedType = 'Jogador';
|
||||
final TextEditingController nameController = TextEditingController();
|
||||
final TextEditingController numberController = TextEditingController();
|
||||
_showPersonDialog(context, teamId: teamId);
|
||||
}
|
||||
|
||||
// Mostrar Diálogo de Editar
|
||||
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
||||
_showPersonDialog(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}) {
|
||||
final isEditing = person != null;
|
||||
final nameController = TextEditingController(text: person?.name ?? '');
|
||||
final numberController = TextEditingController(text: person?.number ?? '');
|
||||
|
||||
// Valor inicial do dropdown ('Jogador' por defeito)
|
||||
String selectedType = person?.type ?? 'Jogador';
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
// Usamos StatefulBuilder para atualizar o Dropdown dentro do Dialog
|
||||
return StatefulBuilder(
|
||||
builder: (context, setPopupState) {
|
||||
builder: (context, setState) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
|
||||
title: const Text('Novo Personagem'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Seletor: Jogador ou Treinador
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedType,
|
||||
decoration: const InputDecoration(labelText: 'Tipo'),
|
||||
items: ['Jogador', 'Treinador']
|
||||
.map((t) => DropdownMenuItem(value: t, child: Text(t)))
|
||||
.toList(),
|
||||
onChanged: (val) {
|
||||
setPopupState(() {
|
||||
selectedType = val!;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
// Campo Nome
|
||||
TextField(
|
||||
controller: nameController,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nome Completo',
|
||||
hintText: 'Ex: Stephen Curry',
|
||||
),
|
||||
),
|
||||
// Campo Número (Aparece apenas se for Jogador)
|
||||
if (selectedType == 'Jogador') ...[
|
||||
const SizedBox(height: 15),
|
||||
TextField(
|
||||
controller: numberController,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Número da Camisola',
|
||||
hintText: 'Ex: 30',
|
||||
),
|
||||
),
|
||||
title: Text(isEditing ? 'Editar Membro' : 'Novo Membro'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Nome
|
||||
TextField(
|
||||
controller: nameController,
|
||||
decoration: const InputDecoration(labelText: 'Nome'),
|
||||
textCapitalization: TextCapitalization.sentences,
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// Tipo (Jogador/Treinador)
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedType,
|
||||
decoration: const InputDecoration(labelText: 'Função'),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'Jogador', child: Text('Jogador')),
|
||||
DropdownMenuItem(value: 'Treinador', child: Text('Treinador')),
|
||||
],
|
||||
],
|
||||
),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() => selectedType = value);
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// Número (Só aparece se for Jogador)
|
||||
if (selectedType == 'Jogador')
|
||||
TextField(
|
||||
controller: numberController,
|
||||
decoration: const InputDecoration(labelText: 'Número da Camisola'),
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
@@ -148,23 +116,22 @@ void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF00C853),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
|
||||
onPressed: () async {
|
||||
if (nameController.text.isNotEmpty) {
|
||||
// CHAMA A FUNÇÃO DE GRAVAR DO FIREBASE
|
||||
await addPerson(
|
||||
teamId,
|
||||
nameController.text,
|
||||
selectedType,
|
||||
selectedType == 'Jogador' ? numberController.text : '',
|
||||
);
|
||||
|
||||
if (context.mounted) Navigator.pop(context);
|
||||
if (nameController.text.isEmpty) return;
|
||||
|
||||
final name = nameController.text.trim();
|
||||
final number = numberController.text.trim();
|
||||
|
||||
if (isEditing) {
|
||||
await _updatePersonInSupabase(person!.id, name, selectedType, number);
|
||||
} else {
|
||||
await _addPersonToSupabase(teamId, name, selectedType, number);
|
||||
}
|
||||
|
||||
if (context.mounted) Navigator.pop(context);
|
||||
},
|
||||
child: const Text('Guardar', style: TextStyle(color: Colors.white)),
|
||||
child: Text(isEditing ? 'Guardar' : 'Adicionar', style: const TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user