Files
LearnIT/lib/features/materials/presentation/pages/teacher_materials_page.dart
2026-05-16 14:52:53 +01:00

658 lines
19 KiB
Dart

import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:file_selector/file_selector.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as path;
import '../../../../core/services/auth_service.dart';
/// Página de Materiais do Professor
/// Tela dedicada para upload e gestão de materiais para a IA
class TeacherMaterialsPage extends StatefulWidget {
const TeacherMaterialsPage({super.key});
@override
State<TeacherMaterialsPage> createState() => _TeacherMaterialsPageState();
}
class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseStorage _storage = FirebaseStorage.instanceFor(
bucket: 'teachit-app.firebasestorage.app',
);
final ImagePicker _imagePicker = ImagePicker();
bool _isUploading = false;
Stream<QuerySnapshot> _getMaterialsStream() {
final currentUser = AuthService.currentUser;
if (currentUser == null) {
return const Stream.empty();
}
return _firestore
.collection('materials')
.where('teacherId', isEqualTo: currentUser.uid)
.orderBy('createdAt', descending: true)
.snapshots();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'Materiais da Turma',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
backgroundColor: const Color(0xFF82C9BD),
elevation: 0,
iconTheme: const IconThemeData(color: Colors.white),
),
floatingActionButton: _isUploading
? FloatingActionButton.extended(
onPressed: null,
backgroundColor: const Color(0xFFF68D2D).withOpacity(0.6),
icon: const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
),
label: const Text(
'A enviar...',
style: TextStyle(color: Colors.white),
),
)
: FloatingActionButton.extended(
onPressed: _showUploadOptions,
backgroundColor: const Color(0xFFF68D2D),
icon: const Icon(Icons.add, color: Colors.white),
label: const Text(
'Adicionar',
style: TextStyle(color: Colors.white),
),
),
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF82C9BD),
Color(0xFFF8F9FA),
],
stops: [0.0, 0.4],
),
),
child: SafeArea(
child: StreamBuilder<QuerySnapshot>(
stream: _getMaterialsStream(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(
color: Color(0xFF82C9BD),
),
);
}
if (snapshot.hasError) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
color: Colors.red,
size: 48,
),
const SizedBox(height: 16),
Text(
'Erro ao carregar materiais:\n${snapshot.error}',
textAlign: TextAlign.center,
style: const TextStyle(
color: Color(0xFF2D3748),
fontSize: 16,
),
),
],
),
);
}
final materials = snapshot.data?.docs ?? [];
if (materials.isEmpty) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.folder_open,
color: Color(0xFF718096),
size: 64,
),
SizedBox(height: 16),
Text(
'Nenhum material enviado ainda.',
style: TextStyle(
color: Color(0xFF718096),
fontSize: 16,
),
),
SizedBox(height: 8),
Text(
'Os materiais enviados aparecerão aqui.',
style: TextStyle(
color: Color(0xFF9CA3AF),
fontSize: 14,
),
),
],
),
);
}
return ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: materials.length,
itemBuilder: (context, index) {
final material = materials[index].data() as Map<String, dynamic>;
final fileName = material['fileName'] ?? 'Ficheiro sem nome';
final createdAt = material['createdAt'] as Timestamp?;
// Inferir tipo pela extensão do filename
final extension = path.extension(fileName).toLowerCase();
final fileType = extension == '.pdf' ? 'pdf' :
(extension == '.jpg' || extension == '.jpeg' || extension == '.png') ? 'image' : 'other';
final docId = materials[index].id;
final url = material['url'] as String?;
return _buildMaterialCard(
docId: docId,
fileName: fileName,
fileType: fileType,
createdAt: createdAt,
url: url,
);
},
);
},
),
),
),
);
}
void _showUploadOptions() {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) => Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
child: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Container(
width: 40,
height: 4,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(2),
),
),
),
const SizedBox(height: 20),
const Text(
'Adicionar Material',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF2D3748),
),
),
const SizedBox(height: 20),
_buildUploadOption(
icon: Icons.picture_as_pdf,
color: Colors.red,
title: 'PDF',
subtitle: 'Selecionar ficheiro PDF',
onTap: () {
Navigator.pop(context);
_selectPDF();
},
),
const SizedBox(height: 12),
_buildUploadOption(
icon: Icons.image,
color: Colors.blue,
title: 'Imagem da Galeria',
subtitle: 'Escolher foto existente',
onTap: () {
Navigator.pop(context);
_selectImageFromGallery();
},
),
const SizedBox(height: 12),
_buildUploadOption(
icon: Icons.camera_alt,
color: const Color(0xFF82C9BD),
title: 'Foto da Câmara',
subtitle: 'Tirar foto nova',
onTap: () {
Navigator.pop(context);
_takePhotoWithCamera();
},
),
const SizedBox(height: 20),
],
),
),
),
),
);
}
Widget _buildUploadOption({
required IconData icon,
required Color color,
required String title,
required String subtitle,
required VoidCallback onTap,
}) {
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(12),
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: color.withOpacity(0.05),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: color.withOpacity(0.2),
width: 1,
),
),
child: Row(
children: [
Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: color.withOpacity(0.1),
borderRadius: BorderRadius.circular(12),
),
child: Icon(
icon,
color: color,
size: 24,
),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Color(0xFF2D3748),
),
),
const SizedBox(height: 2),
Text(
subtitle,
style: TextStyle(
fontSize: 13,
color: Colors.grey[600],
),
),
],
),
),
Icon(
Icons.arrow_forward_ios,
color: Colors.grey[400],
size: 16,
),
],
),
),
);
}
Future<void> _selectPDF() async {
try {
const XTypeGroup pdfTypeGroup = XTypeGroup(
label: 'PDFs',
extensions: ['pdf'],
uniformTypeIdentifiers: ['com.adobe.pdf'],
);
final XFile? file = await openFile(acceptedTypeGroups: [pdfTypeGroup]);
if (file == null) return;
await _uploadFile(
filePath: file.path,
fileName: path.basename(file.path),
fileType: 'pdf',
);
} catch (e) {
if (mounted) {
_showErrorSnackBar('Erro ao selecionar PDF: $e');
}
}
}
Future<void> _selectImageFromGallery() async {
try {
final XFile? image = await _imagePicker.pickImage(
source: ImageSource.gallery,
maxWidth: 1920,
maxHeight: 1080,
imageQuality: 85,
);
if (image == null) return;
await _uploadFile(
filePath: image.path,
fileName: path.basename(image.path),
fileType: 'image',
);
} catch (e) {
if (mounted) {
_showErrorSnackBar('Erro ao selecionar imagem: $e');
}
}
}
Future<void> _takePhotoWithCamera() async {
try {
final XFile? photo = await _imagePicker.pickImage(
source: ImageSource.camera,
maxWidth: 1920,
maxHeight: 1080,
imageQuality: 85,
);
if (photo == null) return;
await _uploadFile(
filePath: photo.path,
fileName: path.basename(photo.path),
fileType: 'image',
);
} catch (e) {
if (mounted) {
_showErrorSnackBar('Erro ao tirar foto: $e');
}
}
}
Future<void> _uploadFile({
required String filePath,
required String fileName,
required String fileType,
}) async {
final currentUser = FirebaseAuth.instance.currentUser;
if (currentUser == null) {
_showErrorSnackBar('Utilizador não autenticado');
return;
}
final uid = currentUser.uid;
final cleanFileName = path.basename(fileName);
setState(() => _isUploading = true);
try {
// Upload para Firebase Storage: teachers/{uid}/materials/{fileName}
final storage = FirebaseStorage.instanceFor(
bucket: 'teachit-app.firebasestorage.app',
);
final ref = storage
.ref()
.child('teachers')
.child(uid)
.child('materials')
.child(cleanFileName);
await ref.putFile(File(filePath));
final downloadUrl = await ref.getDownloadURL();
// Criar documento no Firestore
await FirebaseFirestore.instance.collection('materials').add({
'teacherId': uid,
'fileName': cleanFileName,
'url': downloadUrl,
'createdAt': FieldValue.serverTimestamp(),
});
if (mounted) {
_showSuccessSnackBar('Material enviado com sucesso!');
}
} catch (e) {
if (mounted) {
_showErrorSnackBar('Erro ao enviar material: $e');
}
} finally {
if (mounted) {
setState(() => _isUploading = false);
}
}
}
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) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
const Icon(Icons.check_circle, color: Colors.white),
const SizedBox(width: 12),
Expanded(child: Text(message)),
],
),
backgroundColor: const Color(0xFF10B981),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
duration: const Duration(seconds: 2),
),
);
}
void _showErrorSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
const Icon(Icons.error_outline, color: Colors.white),
const SizedBox(width: 12),
Expanded(child: Text(message)),
],
),
backgroundColor: Colors.red,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
duration: const Duration(seconds: 3),
),
);
}
Widget _buildMaterialCard({
required String docId,
required String fileName,
required String fileType,
required Timestamp? createdAt,
String? url,
}) {
IconData iconData;
Color iconColor;
switch (fileType.toLowerCase()) {
case 'pdf':
iconData = Icons.picture_as_pdf;
iconColor = Colors.red;
break;
case 'image':
case 'jpg':
case 'jpeg':
case 'png':
iconData = Icons.image;
iconColor = Colors.blue;
break;
default:
iconData = Icons.insert_drive_file;
iconColor = const Color(0xFF82C9BD);
}
String formattedDate = 'Data desconhecida';
if (createdAt != null) {
formattedDate = DateFormat('dd/MM/yyyy HH:mm').format(createdAt.toDate());
}
return Card(
margin: const EdgeInsets.only(bottom: 12),
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
leading: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: iconColor.withOpacity(0.1),
borderRadius: BorderRadius.circular(10),
),
child: Icon(
iconData,
color: iconColor,
size: 28,
),
),
title: Text(
fileName,
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
color: Color(0xFF2D3748),
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
subtitle: Text(
formattedDate,
style: const TextStyle(
color: Color(0xFF718096),
fontSize: 13,
),
),
trailing: IconButton(
icon: const Icon(Icons.delete_outline, color: Colors.red),
tooltip: 'Eliminar',
onPressed: () => _showDeleteConfirmation(
docId: docId,
fileName: fileName,
url: url,
),
),
),
);
}
}