From 71d1aca6b51c0d65fa1069da297eaf9225db8269 Mon Sep 17 00:00:00 2001 From: Vishesh Handa <me@vhanda.in> Date: Mon, 21 May 2018 17:16:21 +0200 Subject: [PATCH] Implement note deletion It looks quite ugly, but it works. --- lib/app.dart | 1 + lib/file_storage.dart | 3 ++- lib/journal_list.dart | 23 +++++++++++++++++++++-- lib/screens/home_screen.dart | 3 +++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index f479c744..ab4a8986 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -44,6 +44,7 @@ class JournalAppState extends State<JournalApp> { home: new HomeScreen( appState: appState, noteAdder: addNote, + noteRemover: removeNote, ), theme: new ThemeData( brightness: Brightness.dark, diff --git a/lib/file_storage.dart b/lib/file_storage.dart index 9d7fe467..1622fb41 100644 --- a/lib/file_storage.dart +++ b/lib/file_storage.dart @@ -37,7 +37,8 @@ class FileStorage { Future<Directory> saveNotes(List<Note> notes) async { final dir = await getDirectory(); - //await dir.delete(recursive: true); + await dir.delete(recursive: true); + await dir.create(); for (var note in notes) { var filePath = p.join(dir.path, note.id); diff --git a/lib/journal_list.dart b/lib/journal_list.dart index 18cdf017..2d777b1c 100644 --- a/lib/journal_list.dart +++ b/lib/journal_list.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter/foundation.dart'; import 'package:intl/intl.dart'; import 'note.dart'; @@ -7,11 +8,16 @@ typedef void NoteSelectedFunction(Note note); class JournalList extends StatelessWidget { final NoteSelectedFunction noteSelectedFunction; + final NoteRemover noteRemover; final List<Note> notes; final _biggerFont = const TextStyle(fontSize: 18.0); - JournalList({this.noteSelectedFunction, this.notes}); + JournalList({ + @required this.notes, + @required this.noteSelectedFunction, + @required this.noteRemover, + }); @override Widget build(BuildContext context) { @@ -22,7 +28,20 @@ class JournalList extends StatelessWidget { return null; } //if (i.isOdd) return new Divider(); - return _buildRow(context, notes[i]); + + var note = notes[i]; + return new Dismissible( + key: new Key(note.id), + child: _buildRow(context, note), + background: new Container(color: Colors.red), + onDismissed: (direction) { + noteRemover(note); + + Scaffold + .of(context) + .showSnackBar(new SnackBar(content: new Text("Note deleted"))); + }, + ); }, ); } diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 11bbd795..eb781f92 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -9,10 +9,12 @@ import 'package:journal/note_viewer.dart'; class HomeScreen extends StatelessWidget { final AppState appState; final NoteAdder noteAdder; + final NoteRemover noteRemover; HomeScreen({ @required this.appState, @required this.noteAdder, + @required this.noteRemover, }); @override @@ -47,6 +49,7 @@ class HomeScreen extends StatelessWidget { body: new JournalList( notes: appState.notes, noteSelectedFunction: (note) => _noteSelected(note, context), + noteRemover: noteRemover, ), ); }