Visualização de conteudo

This commit is contained in:
2026-05-19 21:54:37 +01:00
parent 54d7042b94
commit 80ed2b1346
13 changed files with 1121 additions and 431 deletions

View File

@@ -64,11 +64,13 @@ class MaterialsRAGService {
if (classId == null || enrolledClassIds.contains(classId)) { if (classId == null || enrolledClassIds.contains(classId)) {
final fileName = data['fileName'] as String? ?? 'Material'; final fileName = data['fileName'] as String? ?? 'Material';
final teacherId = data['teacherId'] as String?; final teacherId = data['teacherId'] as String?;
final url = data['url'] as String?;
result.add({ result.add({
'id': doc.id, 'id': doc.id,
'name': fileName, 'name': fileName,
if (classId != null) 'classId': classId, if (classId != null) 'classId': classId,
if (teacherId != null) 'teacherId': teacherId, if (teacherId != null) 'teacherId': teacherId,
if (url != null) 'url': url,
}); });
} }
} }
@@ -598,16 +600,24 @@ class MaterialsRAGService {
} }
/// Obter o texto completo de um PDF específico para pré-visualização /// Obter o texto completo de um PDF específico para pré-visualização
static Future<String> getFullPDFText(String fileName, String teacherId) async { static Future<String> getFullPDFText(
String fileName,
String teacherId,
) async {
try { try {
// Remover extensão se existir // Remover extensão se existir
final cleanFileName = fileName.endsWith('.pdf') ? fileName : '$fileName.pdf'; final cleanFileName = fileName.endsWith('.pdf')
? fileName
: '$fileName.pdf';
// Usar cache do texto completo se disponível // Usar cache do texto completo se disponível
final cacheKey = '${cleanFileName}_preview_v6'; final cacheKey = '${cleanFileName}_preview_v6';
if (_chunksCache.containsKey(cacheKey) && _chunksCache[cacheKey]!.isNotEmpty) { if (_chunksCache.containsKey(cacheKey) &&
_chunksCache[cacheKey]!.isNotEmpty) {
final fullText = _chunksCache[cacheKey]!.first; final fullText = _chunksCache[cacheKey]!.first;
Logger.info('Using cached preview text for $cleanFileName: ${fullText.length} chars'); Logger.info(
'Using cached preview text for $cleanFileName: ${fullText.length} chars',
);
return fullText; return fullText;
} }
@@ -620,7 +630,9 @@ class MaterialsRAGService {
// Guardar em cache // Guardar em cache
_chunksCache[cacheKey] = [formattedText]; _chunksCache[cacheKey] = [formattedText];
Logger.info('PDF "$cleanFileName" -> ${formattedText.length} chars extracted and formatted for preview'); Logger.info(
'PDF "$cleanFileName" -> ${formattedText.length} chars extracted and formatted for preview',
);
return formattedText; return formattedText;
} catch (e) { } catch (e) {
Logger.error('Error getting full PDF text for $fileName: $e'); Logger.error('Error getting full PDF text for $fileName: $e');

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart'; import 'package:flutter_animate/flutter_animate.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import '../../../classes/presentation/pages/join_class_page.dart'; import '../../../classes/presentation/pages/join_class_page.dart';
import '../../../materials/presentation/pages/content_management_page.dart';
import 'dashboard_action_card.dart'; import 'dashboard_action_card.dart';
/// Quick access cards for Student Dashboard with horizontal scrollable row /// Quick access cards for Student Dashboard with horizontal scrollable row
@@ -22,6 +23,7 @@ class QuickAccessWidget extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final cards = [ final cards = [
_buildTutorIACard(context), _buildTutorIACard(context),
_buildContentManagementCard(context),
_buildQuizCard(context), _buildQuizCard(context),
_buildAchievementsCard(context), _buildAchievementsCard(context),
]; ];
@@ -69,6 +71,8 @@ class QuickAccessWidget extends StatelessWidget {
SizedBox(width: _scrollCardWidth, child: cards[1]), SizedBox(width: _scrollCardWidth, child: cards[1]),
const SizedBox(width: 12), const SizedBox(width: 12),
SizedBox(width: _scrollCardWidth, child: cards[2]), SizedBox(width: _scrollCardWidth, child: cards[2]),
const SizedBox(width: 12),
SizedBox(width: _scrollCardWidth, child: cards[3]),
], ],
), ),
), ),
@@ -114,6 +118,37 @@ class QuickAccessWidget extends StatelessWidget {
); );
} }
Widget _buildContentManagementCard(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(16),
child:
DashboardActionCardSurface(
title: 'Gerenciamento Conteúdo',
subtitle: 'Ver por disciplinas',
icon: Icons.folder_open,
minHeight: _cardMinHeight,
titleFontSize: _titleFontSize,
subtitleFontSize: _subtitleFontSize,
iconSize: _iconSize,
padding: _cardPadding,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const ContentManagementPage(),
),
);
},
)
.animate()
.fadeIn(
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
)
.then(delay: const Duration(milliseconds: 150)),
);
}
Widget _buildQuizCard(BuildContext context) { Widget _buildQuizCard(BuildContext context) {
return ClipRRect( return ClipRRect(
borderRadius: BorderRadius.circular(16), borderRadius: BorderRadius.circular(16),
@@ -197,6 +232,18 @@ class QuickAccessWidget extends StatelessWidget {
context.go('/ai-tutor'); context.go('/ai-tutor');
}, },
), ),
_QuickAccessItem(
title: 'Gerenciamento Conteúdo',
subtitle: 'Ver por disciplinas',
icon: Icons.folder_open,
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(builder: (_) => const ContentManagementPage()),
);
},
),
_QuickAccessItem( _QuickAccessItem(
title: 'Quiz', title: 'Quiz',
subtitle: 'Testa os teus conhecimentos', subtitle: 'Testa os teus conhecimentos',

View File

@@ -0,0 +1,714 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as path;
import 'package:url_launcher/url_launcher.dart';
import '../../../../core/theme/app_theme_extension.dart';
import 'pdf_viewer_page.dart';
/// Página de Gerenciamento de Conteúdo
/// Mostra todos os materiais disponíveis separados por disciplinas (turmas)
class ContentManagementPage extends StatefulWidget {
const ContentManagementPage({super.key});
@override
State<ContentManagementPage> createState() => _ContentManagementPageState();
}
class _ContentManagementPageState extends State<ContentManagementPage> {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
Map<String, List<Map<String, dynamic>>> _materialsByClass = {};
Map<String, String> _classNames = {};
bool _isLoading = true;
String _searchQuery = '';
final TextEditingController _searchController = TextEditingController();
List<String> _filteredClassIds = [];
int _selectedTabIndex = 0;
@override
void initState() {
super.initState();
_loadMaterials();
}
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
void _filterClasses(String query) {
setState(() {
_searchQuery = query.toLowerCase();
if (_searchQuery.isEmpty) {
_filteredClassIds = _classNames.keys.toList();
} else {
_filteredClassIds = _classNames.entries
.where((entry) => entry.value.toLowerCase().contains(_searchQuery))
.map((entry) => entry.key)
.toList();
}
_selectedTabIndex = 0;
});
}
Future<void> _loadMaterials() async {
final currentUser = FirebaseAuth.instance.currentUser;
if (currentUser == null) {
if (mounted) setState(() => _isLoading = false);
return;
}
try {
// Buscar turmas onde o aluno está inscrito
final enrollSnapshot = await _firestore
.collection('enrollments')
.where('studentId', isEqualTo: currentUser.uid)
.get();
final enrolledClassIds = enrollSnapshot.docs
.map((doc) => doc.data()['classId'] as String?)
.whereType<String>()
.toSet();
if (enrolledClassIds.isEmpty) {
if (mounted) setState(() => _isLoading = false);
return;
}
// Buscar nomes das turmas
final classNames = <String, String>{};
final classDocs = await Future.wait(
enrolledClassIds.map(
(id) => _firestore.collection('classes').doc(id).get(),
),
);
for (final doc in classDocs.where((d) => d.exists)) {
classNames[doc.id] = doc.data()?['name'] as String? ?? doc.id;
}
// Buscar materiais dessas turmas (uma por uma para evitar índice)
final materialsByClass = <String, List<Map<String, dynamic>>>{};
for (final classId in enrolledClassIds) {
final materialsSnapshot = await _firestore
.collection('materials')
.where('classId', isEqualTo: classId)
.get();
for (final doc in materialsSnapshot.docs) {
final material = doc.data();
material['id'] = doc.id;
if (classNames.containsKey(classId)) {
materialsByClass.putIfAbsent(classId, () => []).add(material);
}
}
}
if (mounted) {
setState(() {
_materialsByClass = materialsByClass;
_classNames = classNames;
_filteredClassIds = classNames.keys.toList();
_isLoading = false;
});
}
} catch (e) {
print('Error loading materials: $e');
if (mounted) setState(() => _isLoading = false);
}
}
Future<void> _viewInApp(String url, String fileName) async {
if (url.isEmpty) {
_showErrorSnackBar('URL do ficheiro não disponível');
return;
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PdfViewerPage(url: url, fileName: fileName),
),
);
}
Future<void> _openFile(String? url, String fileName) async {
if (url == null || url.isEmpty) {
_showErrorSnackBar('URL do ficheiro não disponível');
return;
}
try {
final uri = Uri.parse(url);
final launched = await launchUrl(
uri,
mode: LaunchMode.externalApplication,
);
if (!launched) {
_showErrorSnackBar('Não foi possível abrir o ficheiro');
}
} catch (e) {
_showErrorSnackBar('Erro ao abrir ficheiro: $e');
}
}
Future<void> _deleteMaterial(
String docId,
String fileName,
String? url,
) async {
// Students cannot delete materials
_showErrorSnackBar('Apenas professores podem eliminar materiais');
}
void _showDeleteConfirmation(String docId, 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, fileName, 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),
),
);
}
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
final themeExtras = AppThemeExtras.of(context);
if (_isLoading) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: themeExtras.dashboardBackgroundGradient,
stops: themeExtras.dashboardGradientStops,
),
),
child: const Center(child: CircularProgressIndicator()),
),
);
}
if (_materialsByClass.isEmpty) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: themeExtras.dashboardBackgroundGradient,
stops: themeExtras.dashboardGradientStops,
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 120,
height: 120,
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.3),
borderRadius: BorderRadius.circular(24),
),
child: Icon(
Icons.folder_open,
color: cs.onSurfaceVariant,
size: 64,
),
)
.animate()
.fadeIn(duration: const Duration(milliseconds: 400))
.scale(),
const SizedBox(height: 24),
Text(
'Nenhum material disponível',
style: TextStyle(
color: cs.onSurface,
fontSize: 18,
fontWeight: FontWeight.bold,
),
).animate().fadeIn(delay: const Duration(milliseconds: 200)),
const SizedBox(height: 8),
Text(
'Junta-te a uma disciplina para ver materiais',
style: TextStyle(color: cs.onSurfaceVariant, fontSize: 14),
).animate().fadeIn(delay: const Duration(milliseconds: 300)),
],
),
),
),
);
}
final defaultTabController = DefaultTabController(
length: _filteredClassIds.length,
initialIndex: _selectedTabIndex,
child: Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: themeExtras.dashboardBackgroundGradient,
stops: themeExtras.dashboardGradientStops,
),
),
child: Column(
children: [
SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Row(
children: [
IconButton(
icon: Icon(Icons.arrow_back, color: cs.onSurface),
onPressed: () => Navigator.pop(context),
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Gerenciamento de Conteúdo',
style: TextStyle(
color: cs.onSurface,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
],
),
const SizedBox(height: 16),
Container(
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.8),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: cs.outline.withOpacity(0.2),
width: 1,
),
),
child: TextField(
controller: _searchController,
decoration: InputDecoration(
hintText: 'Procurar turmas...',
hintStyle: TextStyle(color: cs.onSurfaceVariant),
prefixIcon: Icon(
Icons.search,
color: cs.onSurfaceVariant,
),
suffixIcon: _searchQuery.isNotEmpty
? IconButton(
icon: Icon(
Icons.clear,
color: cs.onSurfaceVariant,
),
onPressed: () {
_searchController.clear();
_filterClasses('');
},
)
: null,
filled: true,
fillColor: Colors.transparent,
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
),
onChanged: _filterClasses,
),
),
],
),
),
),
Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.8),
borderRadius: BorderRadius.circular(16),
),
child: TabBar(
isScrollable: _filteredClassIds.length > 3,
dividerColor: Colors.transparent,
indicatorSize: TabBarIndicatorSize.tab,
indicator: BoxDecoration(
color: cs.primary,
borderRadius: BorderRadius.circular(12),
),
labelColor: cs.onPrimary,
unselectedLabelColor: cs.onSurfaceVariant,
labelStyle: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
),
unselectedLabelStyle: const TextStyle(
fontWeight: FontWeight.normal,
fontSize: 14,
),
tabs: _filteredClassIds
.map(
(classId) => Tab(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(_classNames[classId] ?? classId),
),
),
)
.toList(),
),
),
const SizedBox(height: 16),
Expanded(
child: _filteredClassIds.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 120,
height: 120,
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.3),
borderRadius: BorderRadius.circular(24),
),
child: Icon(
Icons.search_off,
color: cs.onSurfaceVariant,
size: 64,
),
)
.animate()
.fadeIn(
duration: const Duration(milliseconds: 400),
)
.scale(),
const SizedBox(height: 24),
Text(
'Nenhuma turma encontrada',
style: TextStyle(
color: cs.onSurface,
fontSize: 18,
fontWeight: FontWeight.bold,
),
).animate().fadeIn(
delay: const Duration(milliseconds: 200),
),
const SizedBox(height: 8),
Text(
'Tenta uma pesquisa diferente',
style: TextStyle(
color: cs.onSurfaceVariant,
fontSize: 14,
),
).animate().fadeIn(
delay: const Duration(milliseconds: 300),
),
],
),
)
: TabBarView(
children: _filteredClassIds.map((classId) {
final materials = _materialsByClass[classId] ?? [];
if (materials.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.3),
borderRadius: BorderRadius.circular(
20,
),
),
child: Icon(
Icons.folder_open,
color: cs.onSurfaceVariant,
size: 48,
),
)
.animate()
.fadeIn(
duration: const Duration(
milliseconds: 400,
),
)
.scale(),
const SizedBox(height: 20),
Text(
'Nenhum material nesta turma',
style: TextStyle(
color: cs.onSurface,
fontSize: 16,
fontWeight: FontWeight.bold,
),
).animate().fadeIn(
delay: const Duration(milliseconds: 200),
),
],
),
);
}
return ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 16),
itemCount: materials.length,
separatorBuilder: (_, __) =>
const SizedBox(height: 12),
itemBuilder: (context, index) {
final material = materials[index];
return _buildMaterialCard(material, cs)
.animate()
.fadeIn(
delay: Duration(milliseconds: 50 * index),
duration: const Duration(milliseconds: 300),
)
.slideY(
begin: 0.1,
end: 0,
delay: Duration(milliseconds: 50 * index),
);
},
);
}).toList(),
),
),
],
),
),
),
);
return defaultTabController;
}
Widget _buildMaterialCard(Map<String, dynamic> material, ColorScheme cs) {
final fileName = material['fileName'] ?? 'Ficheiro sem nome';
final url = material['url'] as String?;
final createdAt = material['createdAt'] as Timestamp?;
final extension = path.extension(fileName).toLowerCase();
IconData iconData;
Color iconColor;
if (extension == '.pdf') {
iconData = Icons.picture_as_pdf;
iconColor = Colors.red;
} else if (extension == '.jpg' ||
extension == '.jpeg' ||
extension == '.png') {
iconData = Icons.image;
iconColor = Colors.blue;
} else {
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 Container(
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.8),
borderRadius: BorderRadius.circular(16),
border: Border.all(color: cs.outline.withOpacity(0.15), width: 1),
boxShadow: [
BoxShadow(
color: cs.shadow.withOpacity(0.08),
blurRadius: 12,
offset: const Offset(0, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius: BorderRadius.circular(16),
onTap: url != null ? () => _openFile(url, fileName) : null,
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Container(
width: 56,
height: 56,
decoration: BoxDecoration(
color: iconColor.withOpacity(0.15),
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: iconColor.withOpacity(0.3),
width: 1,
),
),
child: Icon(iconData, color: iconColor, size: 28),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
fileName,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: cs.onSurface,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 6),
Row(
children: [
Icon(
Icons.calendar_today_outlined,
size: 12,
color: cs.onSurfaceVariant,
),
const SizedBox(width: 4),
Text(
formattedDate,
style: TextStyle(
color: cs.onSurfaceVariant,
fontSize: 12,
),
),
],
),
],
),
),
if (url != null)
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: cs.primary.withOpacity(0.12),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: cs.primary.withOpacity(0.3),
width: 1,
),
),
child: IconButton(
icon: Icon(
Icons.visibility_outlined,
color: cs.primary,
size: 22,
),
tooltip: 'Ver na app',
onPressed: () => _viewInApp(url, fileName),
),
),
const SizedBox(width: 8),
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: cs.secondary.withOpacity(0.12),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: cs.secondary.withOpacity(0.3),
width: 1,
),
),
child: IconButton(
icon: Icon(
Icons.download_outlined,
color: cs.secondary,
size: 22,
),
tooltip: 'Transferir',
onPressed: () => _openFile(url, fileName),
),
),
],
),
],
),
),
),
),
);
}
}

View File

@@ -0,0 +1,142 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../../../core/theme/app_theme_extension.dart';
class PdfViewerPage extends StatefulWidget {
final String url;
final String fileName;
const PdfViewerPage({super.key, required this.url, required this.fileName});
@override
State<PdfViewerPage> createState() => _PdfViewerPageState();
}
class _PdfViewerPageState extends State<PdfViewerPage> {
late final WebViewController _webViewController;
bool _isLoading = true;
@override
void initState() {
super.initState();
_webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onPageStarted: (String url) {
setState(() {
_isLoading = true;
});
},
onPageFinished: (String url) {
setState(() {
_isLoading = false;
});
},
),
)
..loadRequest(
Uri.parse(
'https://docs.google.com/gview?embedded=true&url=${Uri.encodeComponent(widget.url)}',
),
);
}
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
final themeExtras = AppThemeExtras.of(context);
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: themeExtras.dashboardBackgroundGradient,
stops: themeExtras.dashboardGradientStops,
),
),
child: Column(
children: [
SafeArea(
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: cs.surface.withOpacity(0.8),
boxShadow: [
BoxShadow(
color: cs.shadow.withOpacity(0.1),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Row(
children: [
IconButton(
icon: Icon(Icons.arrow_back, color: cs.onSurface),
onPressed: () => Navigator.pop(context),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.fileName,
style: TextStyle(
color: cs.onSurface,
fontSize: 18,
fontWeight: FontWeight.bold,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
Text(
'Visualização de documento',
style: TextStyle(
color: cs.onSurfaceVariant,
fontSize: 12,
),
),
],
),
),
],
),
),
),
Expanded(
child: Container(
margin: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: cs.surface,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: cs.shadow.withOpacity(0.1),
blurRadius: 12,
offset: const Offset(0, 4),
),
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Stack(
children: [
WebViewWidget(controller: _webViewController),
if (_isLoading)
const Center(child: CircularProgressIndicator()),
],
),
),
),
),
],
),
),
);
}
}

View File

@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import 'package:url_launcher/url_launcher.dart';
import '../../../../core/services/auth_service.dart'; import '../../../../core/services/auth_service.dart';
@@ -825,6 +826,31 @@ class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
); );
} }
Future<void> _openFile(String? url, String fileName) async {
if (url == null || url.isEmpty) {
_showErrorSnackBar('URL do ficheiro não disponível');
return;
}
try {
print('Opening file: $fileName');
print('URL: $url');
final uri = Uri.parse(url);
final launched = await launchUrl(
uri,
mode: LaunchMode.externalApplication,
);
if (!launched) {
_showErrorSnackBar('Não foi possível abrir o ficheiro');
}
} catch (e) {
print('Error opening file: $e');
_showErrorSnackBar('Erro ao abrir ficheiro: $e');
}
}
Widget _buildMaterialCard({ Widget _buildMaterialCard({
required String docId, required String docId,
required String fileName, required String fileName,
@@ -893,14 +919,24 @@ class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
fontSize: 13, fontSize: 13,
), ),
), ),
trailing: IconButton( trailing: Row(
icon: const Icon(Icons.delete_outline, color: Colors.red), mainAxisSize: MainAxisSize.min,
tooltip: 'Eliminar', children: [
onPressed: () => _showDeleteConfirmation( IconButton(
docId: docId, icon: const Icon(Icons.visibility_outlined, color: Colors.blue),
fileName: fileName, tooltip: 'Ver',
url: url, onPressed: () => _openFile(url, fileName),
), ),
IconButton(
icon: const Icon(Icons.delete_outline, color: Colors.red),
tooltip: 'Eliminar',
onPressed: () => _showDeleteConfirmation(
docId: docId,
fileName: fileName,
url: url,
),
),
],
), ),
), ),
); );

View File

@@ -4,11 +4,13 @@ import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../../core/services/materials_rag_service.dart'; import '../../../../core/services/materials_rag_service.dart';
import '../../../../core/services/rag_ai_service.dart'; import '../../../../core/services/rag_ai_service.dart';
import '../../../../core/services/gamification_service.dart'; import '../../../../core/services/gamification_service.dart';
import '../../../../core/utils/logger.dart'; import '../../../../core/utils/logger.dart';
import '../../../../core/theme/app_theme_extension.dart'; import '../../../../core/theme/app_theme_extension.dart';
import '../../../materials/presentation/pages/pdf_viewer_page.dart';
class QuizListPage extends StatefulWidget { class QuizListPage extends StatefulWidget {
const QuizListPage({super.key}); const QuizListPage({super.key});
@@ -742,390 +744,46 @@ class _QuizListPageState extends State<QuizListPage>
dynamic _jsonDecode(String s) => jsonDecode(s); dynamic _jsonDecode(String s) => jsonDecode(s);
Future<void> _previewPDF(Map<String, String> mat, String name) async { Future<void> _openFile(String? url, String fileName) async {
final matId = mat['id']!; if (url == null || url.isEmpty) {
final matName = name.replaceAll('.pdf', '').replaceAll('_', ' '); _showSnack('URL do ficheiro não disponível');
return;
// Mostrar loading }
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
content: Row(
children: [
CircularProgressIndicator(),
const SizedBox(width: 16),
Text('A carregar PDF...'),
],
),
),
);
try { try {
// Obter o texto completo do PDF usando o método existente print('Opening file: $fileName');
final teacherId = mat['teacherId']; print('URL: $url');
if (teacherId == null) { final uri = Uri.parse(url);
Navigator.of(context).pop();
_showSnack(
'Erro: não foi possível identificar o professor do material.',
);
return;
}
final fullText = await MaterialsRAGService.getFullPDFText( final launched = await launchUrl(
matName, uri,
teacherId, mode: LaunchMode.externalApplication,
); );
Navigator.of(context).pop(); // Fechar loading if (!launched) {
_showSnack('Não foi possível abrir o ficheiro');
if (fullText.isEmpty) {
_showSnack('Não foi possível carregar o conteúdo do PDF.');
return;
} }
// Mostrar o conteúdo em um diálogo scrollável melhorado
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.90,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.1),
blurRadius: 20,
offset: const Offset(0, -4),
),
],
),
child: Column(
children: [
// Handle bar
Container(
width: 40,
height: 4,
margin: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: Theme.of(
context,
).colorScheme.onSurfaceVariant.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(2),
),
),
// Header melhorado
Container(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 16,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Theme.of(context).colorScheme.primary,
Theme.of(
context,
).colorScheme.primary.withValues(alpha: 0.8),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: const BorderRadius.vertical(
top: Radius.circular(24),
),
),
child: Column(
children: [
Row(
children: [
Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: Colors.white.withValues(alpha: 0.3),
),
),
child: Icon(
Icons.picture_as_pdf,
color: Colors.white,
size: 24,
),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
matName,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
Text(
'Pré-visualização do conteúdo completo',
style: TextStyle(
fontSize: 13,
color: Colors.white.withValues(alpha: 0.9),
),
),
],
),
),
Container(
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(12),
),
child: IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: const Icon(
Icons.close,
color: Colors.white,
size: 20,
),
),
),
],
),
const SizedBox(height: 16),
// Stats bar
Container(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: Colors.white.withValues(alpha: 0.2),
),
),
child: Row(
children: [
Icon(
Icons.description,
color: Colors.white.withValues(alpha: 0.9),
size: 18,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'${(fullText.length / 1000).toStringAsFixed(1)}K caracteres • ${fullText.split('\n').length} linhas',
style: TextStyle(
fontSize: 12,
color: Colors.white.withValues(alpha: 0.9),
fontWeight: FontWeight.w500,
),
),
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'PDF',
style: TextStyle(
fontSize: 10,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
],
),
),
// Content area melhorado
Expanded(
child: Container(
margin: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Theme.of(
context,
).colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: Theme.of(
context,
).colorScheme.outline.withValues(alpha: 0.1),
),
),
child: Column(
children: [
// Content header
Container(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 16,
),
decoration: BoxDecoration(
color: Theme.of(
context,
).colorScheme.surfaceContainerHighest,
borderRadius: const BorderRadius.vertical(
top: Radius.circular(16),
),
),
child: Row(
children: [
Icon(
Icons.text_fields,
color: Theme.of(context).colorScheme.primary,
size: 18,
),
const SizedBox(width: 8),
Text(
'Conteúdo do Material',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurface,
),
),
const Spacer(),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: Theme.of(
context,
).colorScheme.primary.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'Selecionável',
style: TextStyle(
fontSize: 10,
color: Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
// Text content
Expanded(
child: Container(
padding: const EdgeInsets.all(20),
child: SingleChildScrollView(
child: SelectableText(
fullText,
style: TextStyle(
fontSize: 14,
height: 1.7,
color: Theme.of(context).colorScheme.onSurface,
fontFamily: 'Roboto',
letterSpacing: 0.3,
),
),
),
),
),
],
),
),
),
// Footer melhorado
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest,
borderRadius: const BorderRadius.vertical(
bottom: Radius.circular(24),
),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(
context,
).colorScheme.primary.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(
Icons.info_outline,
color: Theme.of(context).colorScheme.primary,
size: 16,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Texto extraído automaticamente do PDF',
style: TextStyle(
fontSize: 12,
color: Theme.of(
context,
).colorScheme.onSurfaceVariant,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 2),
Text(
'Formatação otimizada para melhor legibilidade',
style: TextStyle(
fontSize: 11,
color: Theme.of(context)
.colorScheme
.onSurfaceVariant
.withValues(alpha: 0.8),
),
),
],
),
),
Icon(
Icons.keyboard_arrow_up,
color: Theme.of(context).colorScheme.onSurfaceVariant,
size: 16,
),
],
),
),
],
),
),
);
} catch (e) { } catch (e) {
Logger.error('Error previewing PDF: $e'); print('Error opening file: $e');
Navigator.of(context).pop(); // Fechar loading _showSnack('Erro ao abrir ficheiro: $e');
_showSnack('Erro ao carregar o PDF. Tenta novamente.');
} }
} }
Future<void> _previewPDF(Map<String, String> mat, String name) async {
final url = mat['url'];
if (url == null || url.isEmpty) {
_showSnack('URL do ficheiro não disponível');
return;
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PdfViewerPage(url: url, fileName: name),
),
);
}
void _showInteractiveQuiz( void _showInteractiveQuiz(
String title, String title,
List<_QuizQuestion> questions, { List<_QuizQuestion> questions, {
@@ -1676,37 +1334,42 @@ class _QuizListPageState extends State<QuizListPage>
], ],
), ),
), ),
// Action indicator // Action indicator with view button
Container( Row(
width: 40, mainAxisSize: MainAxisSize.min,
height: 40, children: [
decoration: BoxDecoration( Container(
color: isGenerating width: 40,
? cs.primary.withValues(alpha: 0.1) height: 40,
: cs.surfaceContainerHighest.withValues(alpha: 0.6), decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12), color: isGenerating
border: Border.all( ? cs.primary.withValues(alpha: 0.1)
color: isGenerating : cs.surfaceContainerHighest.withValues(alpha: 0.6),
? cs.primary.withValues(alpha: 0.2) borderRadius: BorderRadius.circular(12),
: cs.outline.withValues(alpha: 0.12), border: Border.all(
), color: isGenerating
), ? cs.primary.withValues(alpha: 0.2)
child: isGenerating : cs.outline.withValues(alpha: 0.12),
? Center(
child: SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(
strokeWidth: 2,
color: cs.primary,
),
),
)
: Icon(
Icons.chevron_right,
color: cs.onSurfaceVariant,
size: 22,
), ),
),
child: isGenerating
? Center(
child: SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(
strokeWidth: 2,
color: cs.primary,
),
),
)
: Icon(
Icons.chevron_right,
color: cs.onSurfaceVariant,
size: 22,
),
),
],
), ),
], ],
), ),

View File

@@ -8,6 +8,7 @@
#include <file_selector_linux/file_selector_plugin.h> #include <file_selector_linux/file_selector_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h> #include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) { void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar = g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
@@ -16,4 +17,7 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar = g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin"); fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar); flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
} }

View File

@@ -5,6 +5,7 @@
list(APPEND FLUTTER_PLUGIN_LIST list(APPEND FLUTTER_PLUGIN_LIST
file_selector_linux file_selector_linux
flutter_secure_storage_linux flutter_secure_storage_linux
url_launcher_linux
) )
list(APPEND FLUTTER_FFI_PLUGIN_LIST list(APPEND FLUTTER_FFI_PLUGIN_LIST

View File

@@ -19,6 +19,7 @@ import google_sign_in_ios
import local_auth_darwin import local_auth_darwin
import shared_preferences_foundation import shared_preferences_foundation
import sqflite_darwin import sqflite_darwin
import url_launcher_macos
import webview_flutter_wkwebview import webview_flutter_wkwebview
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
@@ -36,5 +37,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
LocalAuthPlugin.register(with: registry.registrar(forPlugin: "LocalAuthPlugin")) LocalAuthPlugin.register(with: registry.registrar(forPlugin: "LocalAuthPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin")) SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "WebViewFlutterPlugin")) WebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "WebViewFlutterPlugin"))
} }

View File

@@ -1673,6 +1673,70 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.4.0" version: "1.4.0"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
sha256: f6a7e5c4835bb4e3026a04793a4199ca2d14c739ec378fdfe23fc8075d0439f8
url: "https://pub.dev"
source: hosted
version: "6.3.2"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
sha256: "3bb000251e55d4a209aa0e2e563309dc9bb2befea2295fd0cec1f51760aac572"
url: "https://pub.dev"
source: hosted
version: "6.3.29"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
sha256: "580fe5dfb51671ae38191d316e027f6b76272b026370708c2d898799750a02b0"
url: "https://pub.dev"
source: hosted
version: "6.4.1"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
sha256: d5e14138b3bc193a0f63c10a53c94b91d399df0512b1f29b94a043db7482384a
url: "https://pub.dev"
source: hosted
version: "3.2.2"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
sha256: "368adf46f71ad3c21b8f06614adb38346f193f3a59ba8fe9a2fd74133070ba18"
url: "https://pub.dev"
source: hosted
version: "3.2.5"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
sha256: "85c81589622fbc87c1c683aaea164d3604a7777495a79d91e39ffcdec39ddb34"
url: "https://pub.dev"
source: hosted
version: "2.4.3"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
sha256: "712c70ab1b99744ff066053cbe3e80c73332b38d46e5e945c98689b2e66fc15f"
url: "https://pub.dev"
source: hosted
version: "3.1.5"
uuid: uuid:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@@ -69,6 +69,7 @@ dependencies:
http: ^1.1.2 http: ^1.1.2
connectivity_plus: ^5.0.2 connectivity_plus: ^5.0.2
retry: ^3.1.2 retry: ^3.1.2
url_launcher: ^6.2.5
# Local Storage # Local Storage
shared_preferences: ^2.2.2 shared_preferences: ^2.2.2

View File

@@ -15,6 +15,7 @@
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h> #include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <local_auth_windows/local_auth_plugin.h> #include <local_auth_windows/local_auth_plugin.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h> #include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) { void RegisterPlugins(flutter::PluginRegistry* registry) {
CloudFirestorePluginCApiRegisterWithRegistrar( CloudFirestorePluginCApiRegisterWithRegistrar(
@@ -35,4 +36,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("LocalAuthPlugin")); registry->GetRegistrarForPlugin("LocalAuthPlugin"));
PermissionHandlerWindowsPluginRegisterWithRegistrar( PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin")); registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
} }

View File

@@ -12,6 +12,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_windows flutter_secure_storage_windows
local_auth_windows local_auth_windows
permission_handler_windows permission_handler_windows
url_launcher_windows
) )
list(APPEND FLUTTER_FFI_PLUGIN_LIST list(APPEND FLUTTER_FFI_PLUGIN_LIST