// Notes: the list — search box, where you are in the title (tap: All / Pinned / conflicted copies / // folders / tags / Trash), pinned first, newest change first; + = a new note there; tap = the note // (note_editor.dart), long-press = pin, move, trash. ⋮: rename the folder, empty the trash. import 'package:flutter/material.dart'; import '../app/sync_service.dart'; import '../app/theme.dart'; import 'note_editor.dart'; import 'notes_model.dart'; /// Where the list is: all, pinned, conflicts, folder (value), tag (value), trash. typedef NoteFilter = ({String kind, String? value}); class NotesPage extends StatefulWidget { const NotesPage({super.key, required this.sync, this.actions = const []}); final SyncService sync; final List actions; @override State createState() => _NotesPageState(); } class _NotesPageState extends State { NoteFilter filter = (kind: 'all', value: null); final search = TextEditingController(); String get label => switch (filter.kind) { 'pinned' => 'Pinned', 'conflicts' => 'Conflicted copies', 'folder' => filter.value!.split('/').last, 'tag' => '#${filter.value}', 'trash' => 'Trash', _ => 'Notes', }; List shown(List all) { var l = filter.kind == 'trash' ? all.where((n) => n.trashed != null) : all.where((n) => n.trashed == null); l = switch (filter.kind) { 'pinned' => l.where((n) => n.pinned), 'conflicts' => l.where((n) => n.origin != null), 'folder' => l.where((n) => n.folder == filter.value || n.folder.startsWith('${filter.value}/')), 'tag' => l.where((n) => n.tags.contains(filter.value)), _ => l, }; final words = fold(search.text).split(RegExp(r'\s+')).where((w) => w.isNotEmpty).toList(); if (words.isNotEmpty) { l = l.where((n) { final t = fold('${n.body} ${n.folder}'); return words.every(t.contains); }); } final out = l.toList() ..sort((a, b) { if (filter.kind != 'trash' && a.pinned != b.pinned) return a.pinned ? -1 : 1; return (b.trashed ?? b.updated).compareTo(a.trashed ?? a.updated); }); return out; } void _open(Note? n) => Navigator.push(context, MaterialPageRoute(builder: (_) => NoteEditor( sync: widget.sync, id: n?.id, folder: filter.kind == 'folder' ? filter.value! : '', pinned: filter.kind == 'pinned', startText: filter.kind == 'tag' ? '\n\n#${filter.value}' : '', onTag: (t) => setState(() => filter = (kind: 'tag', value: t)), ))); /// The sheet behind the title: where to look. void _where(List all) { final live = all.where((n) => n.trashed == null).toList(); final folders = {}; for (final n in live) { if (n.folder.isEmpty) continue; final parts = n.folder.split('/'); for (var i = 1; i <= parts.length; i++) { final p = parts.take(i).join('/'); folders[p] = (folders[p] ?? 0) + 1; } } final tags = {}; for (final n in live) { for (final t in n.tags) { tags[t] = (tags[t] ?? 0) + 1; } } final pinned = live.where((n) => n.pinned).length, copies = live.where((n) => n.origin != null).length; showModalBottomSheet( context: context, backgroundColor: card, isScrollControlled: true, builder: (c) { Widget item(NoteFilter f, Widget icon, String text, int n, {int depth = 0}) => ListTile( dense: true, contentPadding: EdgeInsets.only(left: 16 + depth * 18.0, right: 16), leading: icon, title: Text(text, overflow: TextOverflow.ellipsis), trailing: n > 0 ? Text('$n', style: const TextStyle(color: muted)) : null, selected: f == filter, selectedColor: accent, onTap: () { Navigator.pop(c); setState(() => filter = f); }, ); final names = folders.keys.toList()..sort(); final tagNames = tags.keys.toList()..sort(); return DraggableScrollableSheet( expand: false, initialChildSize: .6, maxChildSize: .92, builder: (_, scroll) => ListView(controller: scroll, children: [ item((kind: 'all', value: null), const Icon(Icons.notes), 'All notes', live.length), if (pinned > 0) item((kind: 'pinned', value: null), const Icon(Icons.push_pin_outlined), 'Pinned', pinned), if (copies > 0) item((kind: 'conflicts', value: null), const Icon(Icons.warning_amber, color: Color(0xFFFF9A4D)), 'Conflicted copies', copies), if (names.isNotEmpty) const _Cap('Folders'), for (final f in names) item((kind: 'folder', value: f), const Icon(Icons.folder_outlined, size: 20), f.split('/').last, folders[f]!, depth: '/'.allMatches(f).length), if (tagNames.isNotEmpty) ...[ const _Cap('Tags'), Padding( padding: const EdgeInsets.symmetric(horizontal: 14), child: Wrap(spacing: 6, runSpacing: 6, children: [ for (final t in tagNames) ChoiceChip( label: Text('#$t ${tags[t]}'), selected: filter == (kind: 'tag', value: t), onSelected: (_) { Navigator.pop(c); setState(() => filter = (kind: 'tag', value: t)); }, ), ]), ), ], const Divider(color: card2), item((kind: 'trash', value: null), const Icon(Icons.delete_outline), 'Trash', all.length - live.length), const SizedBox(height: 12), ]), ); }, ); } /// Long-press: pin, move, trash (or, in the trash: restore, delete for good). void _actions(Note n) { final s = widget.sync.store; showModalBottomSheet( context: context, backgroundColor: card, builder: (c) => SafeArea(child: Column(mainAxisSize: MainAxisSize.min, children: [ ListTile(title: Text(n.title, style: const TextStyle(fontWeight: FontWeight.bold), overflow: TextOverflow.ellipsis)), if (n.trashed == null) ...[ ListTile( leading: Icon(n.pinned ? Icons.push_pin : Icons.push_pin_outlined), title: Text(n.pinned ? 'Unpin' : 'Pin to the top'), onTap: () { Navigator.pop(c); setNote(s, n.id, {'pinned': n.pinned ? 0 : 1}); widget.sync.changed(); }, ), ListTile( leading: const Icon(Icons.drive_file_move_outlined), title: const Text('Move to folder…'), onTap: () async { Navigator.pop(c); final f = await askFolder(context, widget.sync, n.folder); if (f != null) { setNote(s, n.id, {'folder': cleanFolder(f)}); widget.sync.changed(); } }, ), ListTile( leading: const Icon(Icons.delete_outline), title: const Text('Move to the trash'), onTap: () { Navigator.pop(c); trashNote(s, n.id, true); widget.sync.changed(); ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: const Text('Moved to the trash'), persist: false, // with an action Flutter keeps it up until tapped action: SnackBarAction(label: 'Undo', onPressed: () { trashNote(s, n.id, false); widget.sync.changed(); }), )); }, ), ] else ...[ ListTile( leading: const Icon(Icons.restore), title: const Text('Restore'), onTap: () { Navigator.pop(c); trashNote(s, n.id, false); widget.sync.changed(); }, ), ListTile( leading: const Icon(Icons.delete_forever, color: bad), title: const Text('Delete for good', style: TextStyle(color: bad)), onTap: () { Navigator.pop(c); deleteNote(s, n.id); widget.sync.changed(); }, ), ], ])), ); } Future _renameFolder() async { final old = filter.value!; final to = await askText(context, 'Rename folder', old, hint: 'a/b for a sub-folder'); if (to == null || cleanFolder(to).isEmpty || cleanFolder(to) == old) return; renameFolder(widget.sync.store, old, to); widget.sync.changed(); setState(() => filter = (kind: 'folder', value: cleanFolder(to))); } void _emptyTrash() => showDialog( context: context, builder: (d) => AlertDialog( title: const Text('Empty the trash?'), content: const Text('The notes in it are deleted on every device.'), actions: [ TextButton(onPressed: () => Navigator.pop(d), child: const Text('Cancel')), TextButton(onPressed: () { Navigator.pop(d); emptyTrash(widget.sync.store); widget.sync.changed(); }, child: const Text('Delete them', style: TextStyle(color: bad))), ], ), ); @override Widget build(BuildContext context) => ListenableBuilder( listenable: widget.sync, builder: (context, _) { final all = allNotes(widget.sync.store); final list = shown(all); return Scaffold( appBar: AppBar( title: InkWell( onTap: () => _where(all), child: Row(mainAxisSize: MainAxisSize.min, children: [ Flexible(child: Text(label, overflow: TextOverflow.ellipsis)), const Icon(Icons.arrow_drop_down), ]), ), actions: [ ...widget.actions, if (filter.kind == 'folder' || filter.kind == 'trash') PopupMenuButton( icon: const Icon(Icons.more_vert), color: card2, onSelected: (k) => k == 'rename' ? _renameFolder() : _emptyTrash(), itemBuilder: (_) => [ if (filter.kind == 'folder') const PopupMenuItem(value: 'rename', child: Text('Rename folder')), if (filter.kind == 'trash') const PopupMenuItem(value: 'empty', child: Text('Empty the trash')), ], ), ], ), floatingActionButton: filter.kind == 'trash' ? null : FloatingActionButton( backgroundColor: accent, foregroundColor: bg, tooltip: 'New note', onPressed: () => _open(null), child: const Icon(Icons.add), ), body: Column(children: [ Padding( padding: const EdgeInsets.fromLTRB(12, 4, 12, 6), child: TextField( controller: search, onChanged: (_) => setState(() {}), decoration: InputDecoration( isDense: true, hintText: 'Search notes', prefixIcon: const Icon(Icons.search, size: 20), suffixIcon: search.text.isEmpty ? null : IconButton( icon: const Icon(Icons.close, size: 18), onPressed: () => setState(search.clear), ), filled: true, fillColor: card, border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none), ), ), ), Expanded( child: list.isEmpty ? Center(child: Text( search.text.isNotEmpty ? 'Nothing matches.' : filter.kind == 'trash' ? 'The trash is empty.' : 'No notes here yet — + to write one.', style: const TextStyle(color: muted))) : ListView.builder( padding: const EdgeInsets.fromLTRB(12, 0, 12, 88), itemCount: list.length, itemBuilder: (_, i) => _tile(list[i]), ), ), ]), ); }, ); Widget _tile(Note n) { final snip = noteSnippet(n.body), tags = n.tags.take(4).toList(); final showFolder = filter.kind != 'folder' && n.folder.isNotEmpty; return Card( margin: const EdgeInsets.only(bottom: 8), child: InkWell( borderRadius: BorderRadius.circular(12), onTap: () => _open(n), onLongPress: () => _actions(n), child: Padding( padding: const EdgeInsets.fromLTRB(14, 10, 14, 10), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Row(children: [ if (n.pinned && filter.kind != 'trash') const Padding(padding: EdgeInsets.only(right: 4), child: Icon(Icons.push_pin, size: 15, color: accent)), if (n.origin != null) const Padding(padding: EdgeInsets.only(right: 4), child: Icon(Icons.warning_amber, size: 16, color: Color(0xFFFF9A4D))), Expanded(child: Text(n.title, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold))), const SizedBox(width: 8), Text(when(n.trashed ?? n.updated), style: const TextStyle(color: muted, fontSize: 12)), ]), if (snip.isNotEmpty) Padding( padding: const EdgeInsets.only(top: 3), child: Text(snip, maxLines: 2, overflow: TextOverflow.ellipsis, style: const TextStyle(color: muted, fontSize: 13.5)), ), if (showFolder || tags.isNotEmpty) Padding( padding: const EdgeInsets.only(top: 5), child: Wrap(spacing: 10, children: [ if (showFolder) Text('▸ ${n.folder}', style: const TextStyle(color: Color(0xFF5D6472), fontSize: 12)), for (final t in tags) Text('#$t', style: const TextStyle(color: Color(0xFF5CC8FF), fontSize: 12)), ]), ), ]), ), ), ); } } class _Cap extends StatelessWidget { const _Cap(this.text); final String text; @override Widget build(BuildContext context) => Padding( padding: const EdgeInsets.fromLTRB(16, 14, 16, 4), child: Text(text.toUpperCase(), style: const TextStyle(color: muted, fontSize: 12, fontWeight: FontWeight.bold)), ); } /// "14:05" today, "3 Oct" this year, else "3 Oct 2025". String when(String iso) { final d = DateTime.tryParse(iso)?.toLocal(); if (d == null) return ''; final now = DateTime.now(); const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; if (d.year == now.year && d.month == now.month && d.day == now.day) { return '${d.hour.toString().padLeft(2, '0')}:${d.minute.toString().padLeft(2, '0')}'; } return '${d.day} ${months[d.month - 1]}${d.year == now.year ? '' : ' ${d.year}'}'; } /// A one-line text dialog → the text, or null when cancelled. Future askText(BuildContext context, String title, String value, {String? hint}) { final c = TextEditingController(text: value); return showDialog( context: context, builder: (d) => AlertDialog( title: Text(title), content: TextField(controller: c, autofocus: true, decoration: InputDecoration(hintText: hint), onSubmitted: (v) => Navigator.pop(d, v)), actions: [ TextButton(onPressed: () => Navigator.pop(d), child: const Text('Cancel')), TextButton(onPressed: () => Navigator.pop(d, c.text), child: const Text('OK')), ], ), ); } /// Pick a folder (the ones there are, or type a new one; '' = none) → null when cancelled. Future askFolder(BuildContext context, SyncService sync, String current) { final names = {for (final n in allNotes(sync.store)) if (n.trashed == null && n.folder.isNotEmpty) n.folder}.toList()..sort(); return showModalBottomSheet( context: context, backgroundColor: card, isScrollControlled: true, builder: (c) => SafeArea(child: ConstrainedBox( constraints: BoxConstraints(maxHeight: MediaQuery.of(c).size.height * .7), child: ListView(shrinkWrap: true, children: [ const ListTile(title: Text('Move to folder', style: TextStyle(fontWeight: FontWeight.bold))), ListTile( leading: const Icon(Icons.create_new_folder_outlined), title: const Text('New folder…'), onTap: () async { final f = await askText(c, 'New folder', current, hint: 'a/b for a sub-folder'); if (c.mounted) Navigator.pop(c, f); }, ), ListTile( leading: const Icon(Icons.folder_off_outlined), title: const Text('No folder'), selected: current.isEmpty, selectedColor: accent, onTap: () => Navigator.pop(c, ''), ), for (final f in names) ListTile( leading: const Icon(Icons.folder_outlined), title: Text(f), selected: f == current, selectedColor: accent, onTap: () => Navigator.pop(c, f), ), ]), )), ); }