import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../core/constants.dart'; import '../../providers/notes_provider.dart'; class NotesListScreen extends ConsumerWidget { const NotesListScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final notesAsync = ref.watch(notesProvider); return Scaffold( appBar: AppBar(title: const Text('Notes')), body: notesAsync.when( loading: () => const Center(child: CircularProgressIndicator()), error: (e, _) => Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text('Error: $e'), TextButton( onPressed: () => ref.invalidate(notesProvider), child: const Text('Retry'), ), ], ), ), data: (notes) { if (notes.isEmpty) { return const Center(child: Text('No notes yet. Tap + to create one.')); } return RefreshIndicator( onRefresh: () => ref.refresh(notesProvider.future), child: ListView.separated( itemCount: notes.length, separatorBuilder: (_, _) => const Divider(height: 1), itemBuilder: (context, i) { final note = notes[i]; return ListTile( title: Text(note.title), subtitle: Text( note.updatedAt.toLocal().toString().substring(0, 16), style: Theme.of(context).textTheme.bodySmall, ), onTap: () => context.push( Routes.noteDetail.replaceFirst(':id', '${note.id}'), ), ); }, ), ); }, ), floatingActionButton: FloatingActionButton( heroTag: 'notes_fab', onPressed: () => context.push(Routes.noteNew), child: const Icon(Icons.add), ), ); } }