Botao de remover materiais

This commit is contained in:
2026-05-16 14:52:53 +01:00
parent ba4bb7de88
commit 47aaa163fb

View File

@@ -176,10 +176,15 @@ class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
final fileType = extension == '.pdf' ? 'pdf' : final fileType = extension == '.pdf' ? 'pdf' :
(extension == '.jpg' || extension == '.jpeg' || extension == '.png') ? 'image' : 'other'; (extension == '.jpg' || extension == '.jpeg' || extension == '.png') ? 'image' : 'other';
final docId = materials[index].id;
final url = material['url'] as String?;
return _buildMaterialCard( return _buildMaterialCard(
docId: docId,
fileName: fileName, fileName: fileName,
fileType: fileType, fileType: fileType,
createdAt: createdAt, createdAt: createdAt,
url: url,
); );
}, },
); );
@@ -459,6 +464,75 @@ class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
} }
} }
Future<void> _deleteMaterial({
required String docId,
required String fileName,
String? url,
}) async {
try {
// Apagar ficheiro do Firebase Storage
if (url != null && url.isNotEmpty) {
try {
final ref = _storage.refFromURL(url);
await ref.delete();
} catch (_) {
// Se o ficheiro já não existir no Storage, continuar na mesma
}
}
// Apagar documento no Firestore
await _firestore.collection('materials').doc(docId).delete();
if (mounted) {
_showSuccessSnackBar('Material eliminado com sucesso!');
}
} catch (e) {
if (mounted) {
_showErrorSnackBar('Erro ao eliminar material: $e');
}
}
}
void _showDeleteConfirmation({
required String docId,
required String fileName,
String? url,
}) {
showDialog(
context: context,
builder: (dialogContext) => AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
title: const Text(
'Eliminar Material',
style: TextStyle(fontWeight: FontWeight.bold),
),
content: Text(
'Tens a certeza que queres eliminar "$fileName"?\nEsta ação não pode ser desfeita.',
),
actions: [
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(),
child: const Text('Cancelar'),
),
ElevatedButton(
onPressed: () {
Navigator.of(dialogContext).pop();
_deleteMaterial(docId: docId, fileName: fileName, url: url);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text('Eliminar'),
),
],
),
);
}
void _showSuccessSnackBar(String message) { void _showSuccessSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
@@ -496,9 +570,11 @@ class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
} }
Widget _buildMaterialCard({ Widget _buildMaterialCard({
required String docId,
required String fileName, required String fileName,
required String fileType, required String fileType,
required Timestamp? createdAt, required Timestamp? createdAt,
String? url,
}) { }) {
IconData iconData; IconData iconData;
Color iconColor; Color iconColor;
@@ -566,6 +642,15 @@ class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
fontSize: 13, fontSize: 13,
), ),
), ),
trailing: IconButton(
icon: const Icon(Icons.delete_outline, color: Colors.red),
tooltip: 'Eliminar',
onPressed: () => _showDeleteConfirmation(
docId: docId,
fileName: fileName,
url: url,
),
),
), ),
); );
} }