94 lines
3.5 KiB
Dart
94 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import '../models/team_model.dart'; // Importa o teu modelo
|
|
|
|
class TeamsPage extends StatelessWidget {
|
|
const TeamsPage({super.key});
|
|
|
|
// Função para abrir o Pop-up
|
|
void _showCreateTeamDialog(BuildContext context) {
|
|
final TextEditingController _nameController = TextEditingController();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Nova Equipa'),
|
|
content: TextField(
|
|
controller: _nameController,
|
|
decoration: const InputDecoration(hintText: 'Nome da Equipa'),
|
|
autofocus: true,
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancelar'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
if (_nameController.text.isNotEmpty) {
|
|
// Guarda no Firebase
|
|
await FirebaseFirestore.instance.collection('teams').add({
|
|
'name': _nameController.text,
|
|
'createdAt': FieldValue.serverTimestamp(),
|
|
});
|
|
Navigator.pop(context);
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFFE74C3C)),
|
|
child: const Text('Criar', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
// Removi a AppBar porque a HomeScreen já tem uma, evita barra dupla
|
|
body: StreamBuilder<QuerySnapshot>(
|
|
// Escuta o Firebase em tempo real
|
|
stream: FirebaseFirestore.instance.collection('teams').orderBy('createdAt', descending: true).snapshots(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasError) return const Center(child: Text('Erro ao carregar'));
|
|
if (snapshot.connectionState == ConnectionState.waiting) return const Center(child: CircularProgressIndicator());
|
|
|
|
final docs = snapshot.data!.docs;
|
|
|
|
if (docs.isEmpty) {
|
|
return const Center(child: Text('Nenhuma equipa criada.'));
|
|
}
|
|
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: docs.length,
|
|
itemBuilder: (context, index) {
|
|
final team = Team.fromFirestore(docs[index].data() as Map<String, dynamic>, docs[index].id);
|
|
return Card(
|
|
elevation: 2,
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
child: ListTile(
|
|
leading: const CircleAvatar(
|
|
backgroundColor: Color(0xFFE74C3C),
|
|
child: Icon(Icons.groups, color: Colors.white),
|
|
),
|
|
title: Text(team.name, style: const TextStyle(fontWeight: FontWeight.bold)),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () {
|
|
// Navegar para detalhes da equipa no futuro
|
|
},
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () => _showCreateTeamDialog(context),
|
|
backgroundColor: const Color(0xFFE74C3C),
|
|
child: const Icon(Icons.add, color: Colors.white),
|
|
),
|
|
);
|
|
}
|
|
} |