Modificações no chatbot (dashboard aluno) e analytics (dashboard stor)
This commit is contained in:
@@ -0,0 +1,609 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
/// Inline widget (no Scaffold) showing enrolled students for a class,
|
||||
/// with search, real-time updates, and remove-student functionality.
|
||||
class ClassStudentsInlineWidget extends StatefulWidget {
|
||||
final String classId;
|
||||
final String className;
|
||||
|
||||
const ClassStudentsInlineWidget({
|
||||
super.key,
|
||||
required this.classId,
|
||||
required this.className,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ClassStudentsInlineWidget> createState() =>
|
||||
_ClassStudentsInlineWidgetState();
|
||||
}
|
||||
|
||||
class _ClassStudentsInlineWidgetState extends State<ClassStudentsInlineWidget> {
|
||||
String _searchQuery = '';
|
||||
String? _classCode;
|
||||
late Stream<QuerySnapshot> _enrollmentsStream;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadClassCode();
|
||||
_initStream();
|
||||
}
|
||||
|
||||
void _initStream() {
|
||||
_enrollmentsStream = FirebaseFirestore.instance
|
||||
.collection('enrollments')
|
||||
.where('classId', isEqualTo: widget.classId)
|
||||
.snapshots();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(ClassStudentsInlineWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.classId != widget.classId) {
|
||||
_loadClassCode();
|
||||
_initStream();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadClassCode() async {
|
||||
final doc = await FirebaseFirestore.instance
|
||||
.collection('classes')
|
||||
.doc(widget.classId)
|
||||
.get();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_classCode = doc.data()?['code'] as String? ?? '—';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _removeStudent(
|
||||
BuildContext context,
|
||||
String enrollmentDocId,
|
||||
String studentName,
|
||||
) async {
|
||||
final cs = Theme.of(context).colorScheme;
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
title: const Text('Remover aluno'),
|
||||
content: Text(
|
||||
'Tens a certeza que queres remover "$studentName" desta disciplina?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(false),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(backgroundColor: cs.error),
|
||||
onPressed: () => Navigator.of(ctx).pop(true),
|
||||
child: const Text('Remover'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
try {
|
||||
await FirebaseFirestore.instance
|
||||
.collection('enrollments')
|
||||
.doc(enrollmentDocId)
|
||||
.delete();
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
behavior: SnackBarBehavior.floating,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
|
||||
backgroundColor: cs.primary,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
content: Text(
|
||||
'"$studentName" foi removido da disciplina.',
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Erro ao remover aluno: $e')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cs = Theme.of(context).colorScheme;
|
||||
|
||||
return StreamBuilder<QuerySnapshot>(
|
||||
stream: _enrollmentsStream,
|
||||
builder: (context, snapshot) {
|
||||
// Keep showing previous data while new data loads (prevents flash)
|
||||
final docs = snapshot.data?.docs ?? [];
|
||||
// Sort client-side by joinedAt ascending
|
||||
final enrollments = List<QueryDocumentSnapshot>.from(docs)
|
||||
..sort((a, b) {
|
||||
final aData = a.data() as Map<String, dynamic>;
|
||||
final bData = b.data() as Map<String, dynamic>;
|
||||
final aTs = aData['joinedAt'] as Timestamp?;
|
||||
final bTs = bData['joinedAt'] as Timestamp?;
|
||||
if (aTs == null && bTs == null) return 0;
|
||||
if (aTs == null) return 1;
|
||||
if (bTs == null) return -1;
|
||||
return aTs.compareTo(bTs);
|
||||
});
|
||||
final filtered = _searchQuery.isEmpty
|
||||
? enrollments
|
||||
: enrollments.where((doc) {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
final name = (data['studentName'] as String? ?? '')
|
||||
.toLowerCase();
|
||||
final email = (data['studentEmail'] as String? ?? '')
|
||||
.toLowerCase();
|
||||
final q = _searchQuery.toLowerCase();
|
||||
return name.contains(q) || email.contains(q);
|
||||
}).toList();
|
||||
|
||||
final bottomInset = MediaQuery.of(context).viewInsets.bottom;
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(bottom: bottomInset),
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
children: [
|
||||
// ── Header ──────────────────────────────────────────────────
|
||||
Container(
|
||||
padding: const EdgeInsets.all(18),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [cs.primary, cs.primary.withValues(alpha: 0.8)],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.className,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
snapshot.connectionState ==
|
||||
ConnectionState.waiting
|
||||
? 'A carregar…'
|
||||
: '${enrollments.length} aluno${enrollments.length == 1 ? '' : 's'} inscrito${enrollments.length == 1 ? '' : 's'}',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.85),
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Código da disciplina
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
if (_classCode != null && _classCode != '—') {
|
||||
Clipboard.setData(ClipboardData(text: _classCode!));
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
behavior: SnackBarBehavior.floating,
|
||||
margin: const EdgeInsets.symmetric(
|
||||
horizontal: 20,
|
||||
vertical: 12,
|
||||
),
|
||||
backgroundColor: cs.primary,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
content: const Text(
|
||||
'Código copiado!',
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14,
|
||||
vertical: 10,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.18),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Colors.white.withValues(alpha: 0.3),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
const Text(
|
||||
'Código',
|
||||
style: TextStyle(
|
||||
color: Colors.white70,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
_classCode ?? '…',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 2,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.copy,
|
||||
color: Colors.white.withValues(alpha: 0.7),
|
||||
size: 10,
|
||||
),
|
||||
const SizedBox(width: 3),
|
||||
Text(
|
||||
'copiar',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(
|
||||
alpha: 0.7,
|
||||
),
|
||||
fontSize: 9,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
|
||||
// ── Search bar ──────────────────────────────────────────────
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14,
|
||||
vertical: 10,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Colors.white.withValues(alpha: 0.2),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.search,
|
||||
color: Colors.white.withValues(alpha: 0.7),
|
||||
size: 20,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: TextField(
|
||||
onChanged: (v) =>
|
||||
setState(() => _searchQuery = v.trim()),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 14,
|
||||
),
|
||||
cursorColor: Colors.white,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Pesquisar aluno…',
|
||||
hintStyle: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.5),
|
||||
fontSize: 14,
|
||||
),
|
||||
border: InputBorder.none,
|
||||
isDense: true,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_searchQuery.isNotEmpty)
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _searchQuery = ''),
|
||||
child: Icon(
|
||||
Icons.close,
|
||||
color: Colors.white.withValues(alpha: 0.7),
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
|
||||
// ── List ────────────────────────────────────────────────────
|
||||
Expanded(
|
||||
child:
|
||||
snapshot.connectionState == ConnectionState.waiting &&
|
||||
snapshot.data == null
|
||||
? const Center(
|
||||
child: CircularProgressIndicator(color: Colors.white),
|
||||
)
|
||||
: filtered.isEmpty
|
||||
? _buildEmpty(cs)
|
||||
: ListView.separated(
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: filtered.length,
|
||||
separatorBuilder: (_, __) =>
|
||||
const SizedBox(height: 10),
|
||||
itemBuilder: (context, index) {
|
||||
final doc = filtered[index];
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
final studentName =
|
||||
data['studentName'] as String? ??
|
||||
'Aluno sem nome';
|
||||
final studentEmail =
|
||||
data['studentEmail'] as String? ?? '';
|
||||
final joinedAt = data['joinedAt'] as Timestamp?;
|
||||
final enrollmentDocId = doc.id;
|
||||
|
||||
return _buildStudentCard(
|
||||
cs: cs,
|
||||
enrollmentDocId: enrollmentDocId,
|
||||
studentName: studentName,
|
||||
studentEmail: studentEmail,
|
||||
joinedAt: joinedAt,
|
||||
index: index,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStudentCard({
|
||||
required ColorScheme cs,
|
||||
required String enrollmentDocId,
|
||||
required String studentName,
|
||||
required String studentEmail,
|
||||
required Timestamp? joinedAt,
|
||||
required int index,
|
||||
}) {
|
||||
return Dismissible(
|
||||
key: Key(enrollmentDocId),
|
||||
direction: DismissDirection.endToStart,
|
||||
confirmDismiss: (_) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
title: const Text('Remover aluno'),
|
||||
content: Text(
|
||||
'Tens a certeza que queres remover "$studentName" desta disciplina?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(false),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(backgroundColor: cs.error),
|
||||
onPressed: () => Navigator.of(ctx).pop(true),
|
||||
child: const Text('Remover'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return confirmed ?? false;
|
||||
},
|
||||
onDismissed: (_) async {
|
||||
try {
|
||||
await FirebaseFirestore.instance
|
||||
.collection('enrollments')
|
||||
.doc(enrollmentDocId)
|
||||
.delete();
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
behavior: SnackBarBehavior.floating,
|
||||
margin: const EdgeInsets.symmetric(
|
||||
horizontal: 20,
|
||||
vertical: 12,
|
||||
),
|
||||
backgroundColor: cs.primary,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
content: Text(
|
||||
'"$studentName" foi removido da disciplina.',
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Erro ao remover: $e')));
|
||||
}
|
||||
}
|
||||
},
|
||||
background: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: cs.error.withValues(alpha: 0.85),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 20),
|
||||
child: const Icon(Icons.delete_outline, color: Colors.white, size: 24),
|
||||
),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: Colors.white.withValues(alpha: 0.18)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// Avatar with initial
|
||||
Container(
|
||||
width: 42,
|
||||
height: 42,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
studentName.isNotEmpty ? studentName[0].toUpperCase() : '?',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
// Info
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
studentName,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
if (studentEmail.isNotEmpty) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
studentEmail,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.65),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
if (joinedAt != null) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'Entrou em ${DateFormat('dd/MM/yyyy').format(joinedAt.toDate())}',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.5),
|
||||
fontSize: 11,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
// Remove button
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.person_remove_outlined,
|
||||
color: cs.error.withValues(alpha: 0.85),
|
||||
size: 20,
|
||||
),
|
||||
tooltip: 'Remover aluno',
|
||||
onPressed: () =>
|
||||
_removeStudent(context, enrollmentDocId, studentName),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmpty(ColorScheme cs) {
|
||||
if (_searchQuery.isNotEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.search_off,
|
||||
size: 48,
|
||||
color: Colors.white.withValues(alpha: 0.4),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'Nenhum aluno encontrado',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.7),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.people_outline,
|
||||
size: 56,
|
||||
color: Colors.white.withValues(alpha: 0.35),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
Text(
|
||||
'Nenhum aluno inscrito',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.7),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
'Partilha o código da disciplina com os alunos.',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.45),
|
||||
fontSize: 13,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user