diff --git a/lib/features/dashboard/presentation/widgets/teacher_analytics_preview_widget.dart b/lib/features/dashboard/presentation/widgets/teacher_analytics_preview_widget.dart index b9d2950..25df017 100644 --- a/lib/features/dashboard/presentation/widgets/teacher_analytics_preview_widget.dart +++ b/lib/features/dashboard/presentation/widgets/teacher_analytics_preview_widget.dart @@ -1,12 +1,70 @@ import 'package:flutter/material.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; import '../../../../core/theme/app_theme_extension.dart'; import '../../../../core/services/auth_service.dart'; +import '../../../../core/services/gamification_service.dart'; +import '../../../../core/models/class_stats.dart'; /// Analytics preview section for teacher dashboard -class TeacherAnalyticsPreviewWidget extends StatelessWidget { +class TeacherAnalyticsPreviewWidget extends StatefulWidget { const TeacherAnalyticsPreviewWidget({super.key}); + @override + State createState() => _TeacherAnalyticsPreviewWidgetState(); +} + +class _TeacherAnalyticsPreviewWidgetState extends State { + List _topStudents = []; + bool _loading = true; + + @override + void initState() { + super.initState(); + _loadTopStudents(); + } + + Future _loadTopStudents() async { + try { + final user = AuthService.currentUser; + if (user == null) return; + + // Obter turmas do professor + final classesSnapshot = await FirebaseFirestore.instance + .collection('classes') + .where('teacherId', isEqualTo: user.uid) + .get(); + + List allStudents = []; + + // Buscar ranking de cada turma + for (final classDoc in classesSnapshot.docs) { + final classId = classDoc.id; + // Forçar atualização para obter dados mais recentes + final rankings = await GamificationService.getClassRanking(classId); + allStudents.addAll(rankings); + } + + // Ordenar por score e pegar os top 4 + allStudents.sort((a, b) => b.overallScore.compareTo(a.overallScore)); + final top4 = allStudents.take(4).toList(); + + if (mounted) { + setState(() { + _topStudents = top4; + _loading = false; + }); + } + } catch (e) { + print('Error loading top students: $e'); + if (mounted) { + setState(() { + _loading = false; + }); + } + } + } + @override Widget build(BuildContext context) { final user = AuthService.currentUser; @@ -161,33 +219,31 @@ class TeacherAnalyticsPreviewWidget extends StatelessWidget { const SizedBox(height: 12), // Student List Preview - _buildStudentPerformanceItem( - context, - 'Ana Silva', - 95, - Theme.of(context).colorScheme.tertiary, - ), - const SizedBox(height: 8), - _buildStudentPerformanceItem( - context, - 'João Costa', - 88, - Theme.of(context).colorScheme.primary, - ), - const SizedBox(height: 8), - _buildStudentPerformanceItem( - context, - 'Maria Santos', - 82, - Theme.of(context).colorScheme.primary, - ), - const SizedBox(height: 8), - _buildStudentPerformanceItem( - context, - 'Pedro Lima', - 45, - Theme.of(context).colorScheme.secondary, - ), + if (_loading) + const Center(child: CircularProgressIndicator()) + else if (_topStudents.isEmpty) + const Center( + child: Text( + 'Nenhum aluno encontrado', + style: TextStyle(fontSize: 14), + ), + ) + else + ..._topStudents.asMap().entries.map((entry) { + final index = entry.key; + final student = entry.value; + final color = _getStudentColor(context, index); + + return Padding( + padding: const EdgeInsets.only(bottom: 8), + child: _buildStudentPerformanceItem( + context, + student.studentName, + student.overallScore.toInt(), + color, + ), + ); + }).toList(), const SizedBox(height: 20), @@ -279,6 +335,16 @@ class TeacherAnalyticsPreviewWidget extends StatelessWidget { ); } + Color _getStudentColor(BuildContext context, int index) { + final colors = [ + Theme.of(context).colorScheme.tertiary, + Theme.of(context).colorScheme.primary, + Theme.of(context).colorScheme.primary, + Theme.of(context).colorScheme.secondary, + ]; + return colors[index % colors.length]; + } + Widget _buildStudentPerformanceItem( BuildContext context, String name,