121 lines
4.5 KiB
Dart
121 lines
4.5 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../models/person_model.dart';
|
|
|
|
class StatsController {
|
|
final FirebaseFirestore _db = FirebaseFirestore.instance;
|
|
|
|
// --- LÓGICA DE FIREBASE ---
|
|
|
|
// 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({
|
|
'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());
|
|
}
|
|
|
|
// --- LÓGICA DE INTERFACE (POPUP) ---
|
|
|
|
void showAddPersonDialog(BuildContext context, String teamId) {
|
|
String selectedType = 'Jogador';
|
|
final TextEditingController nameController = TextEditingController();
|
|
final TextEditingController numberController = TextEditingController();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return StatefulBuilder(
|
|
builder: (context, setPopupState) {
|
|
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',
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancelar'),
|
|
),
|
|
ElevatedButton(
|
|
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);
|
|
}
|
|
},
|
|
child: const Text('Guardar', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
} |