Files
flux/lib/features/notes/ui/dashboard_notes_widget.dart

198 lines
6.4 KiB
Dart
Raw Normal View History

2026-05-21 14:43:47 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2026-05-23 11:08:39 +02:00
import 'package:flux/core/routes/routes.dart';
import 'package:flux/core/theme/theme.dart';
2026-05-21 14:43:47 +02:00
import 'package:flux/features/notes/blocs/notes_bloc.dart';
import 'package:flux/features/notes/models/note_model.dart';
import 'package:go_router/go_router.dart'; // Supponendo tu usi GoRouter per la navigazione
class DashboardNotesWidget extends StatelessWidget {
const DashboardNotesWidget({super.key});
@override
Widget build(BuildContext context) {
2026-05-23 11:08:39 +02:00
return Card(
elevation: 0,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: BorderSide(
color: Theme.of(context).dividerColor.withValues(alpha: 0.3),
style: BorderStyle.solid,
2026-05-21 14:43:47 +02:00
),
2026-05-23 11:08:39 +02:00
),
child: InkWell(
onTap: () => context.pushNamed(Routes.notes),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Intestazione del riquadro
Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.yellow.shade700.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(
Icons.sticky_note_2_outlined,
size: 16,
color: Colors.yellow.shade700,
),
),
const SizedBox(width: 12),
Text(
'Le mie Note',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: context.primaryText,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
const SizedBox(height: 12),
2026-05-21 14:43:47 +02:00
2026-05-23 11:08:39 +02:00
// Il corpo del widget collegato al Bloc
BlocBuilder<NotesBloc, NotesState>(
builder: (context, state) {
if (state.status == NotesStatus.loading &&
state.notes.isEmpty) {
return const Center(child: CircularProgressIndicator());
}
2026-05-21 14:43:47 +02:00
2026-05-23 11:08:39 +02:00
if (state.status == NotesStatus.failure) {
return const Center(
child: Text(
'Errore nel caricamento delle note.',
style: TextStyle(color: Colors.red),
),
);
}
2026-05-21 14:43:47 +02:00
2026-05-23 11:08:39 +02:00
if (state.notes.isEmpty) {
return _buildEmptyState(context);
}
2026-05-21 14:43:47 +02:00
2026-05-23 11:08:39 +02:00
// Prendiamo solo le prime 4 note per non intaccare troppo spazio in Dashboard
final displayNotes = state.notes.take(4).toList();
2026-05-21 14:43:47 +02:00
2026-05-23 11:08:39 +02:00
return SizedBox(
height: 140, // Altezza fissa per lo scroll orizzontale
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: displayNotes.length,
itemBuilder: (context, index) {
return _buildMiniPostIt(context, displayNotes[index]);
},
),
);
2026-05-21 14:43:47 +02:00
},
),
2026-05-23 11:08:39 +02:00
],
),
2026-05-21 14:43:47 +02:00
),
2026-05-23 11:08:39 +02:00
),
2026-05-21 14:43:47 +02:00
);
}
Widget _buildMiniPostIt(BuildContext context, NoteModel note) {
return GestureDetector(
onTap: () {
// Vai al form di dettaglio passando l'ID o l'oggetto
2026-05-27 19:38:59 +02:00
context.pushNamed(
Routes.noteForm,
pathParameters: {'id': note.id!},
extra: note,
);
2026-05-21 14:43:47 +02:00
},
child: Container(
width: 140,
margin: const EdgeInsets.only(right: 12, bottom: 8),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: note.flutterColor,
borderRadius: BorderRadius.circular(8),
boxShadow: const [
BoxShadow(
color: Colors.black12,
blurRadius: 4,
offset: Offset(2, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
note.title?.isNotEmpty == true
? note.title!
: 'Senza titolo',
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
color: Colors.black87,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
if (note.isPinned)
const Icon(Icons.push_pin, size: 14, color: Colors.black54),
],
),
const SizedBox(height: 8),
Expanded(
child: Text(
note.content ?? '',
style: const TextStyle(fontSize: 12, color: Colors.black87),
maxLines: 4,
overflow: TextOverflow.ellipsis,
),
),
],
),
),
);
}
Widget _buildEmptyState(BuildContext context) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.grey.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: Colors.grey.withValues(alpha: 0.3),
style: BorderStyle.solid,
),
),
child: Column(
children: [
const Icon(
Icons.sticky_note_2_outlined,
size: 32,
color: Colors.grey,
),
const SizedBox(height: 8),
const Text('Nessuna nota presente.'),
TextButton(
onPressed: () => context.push('/notes/create'),
child: const Text('Creane una ora'),
),
],
),
);
}
}