Implement note deletion

It looks quite ugly, but it works.
This commit is contained in:
Vishesh Handa
2018-05-21 17:16:21 +02:00
parent 973f21a24c
commit 71d1aca6b5
4 changed files with 27 additions and 3 deletions

View File

@ -44,6 +44,7 @@ class JournalAppState extends State<JournalApp> {
home: new HomeScreen( home: new HomeScreen(
appState: appState, appState: appState,
noteAdder: addNote, noteAdder: addNote,
noteRemover: removeNote,
), ),
theme: new ThemeData( theme: new ThemeData(
brightness: Brightness.dark, brightness: Brightness.dark,

View File

@ -37,7 +37,8 @@ class FileStorage {
Future<Directory> saveNotes(List<Note> notes) async { Future<Directory> saveNotes(List<Note> notes) async {
final dir = await getDirectory(); final dir = await getDirectory();
//await dir.delete(recursive: true); await dir.delete(recursive: true);
await dir.create();
for (var note in notes) { for (var note in notes) {
var filePath = p.join(dir.path, note.id); var filePath = p.join(dir.path, note.id);

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'note.dart'; import 'note.dart';
@ -7,11 +8,16 @@ typedef void NoteSelectedFunction(Note note);
class JournalList extends StatelessWidget { class JournalList extends StatelessWidget {
final NoteSelectedFunction noteSelectedFunction; final NoteSelectedFunction noteSelectedFunction;
final NoteRemover noteRemover;
final List<Note> notes; final List<Note> notes;
final _biggerFont = const TextStyle(fontSize: 18.0); final _biggerFont = const TextStyle(fontSize: 18.0);
JournalList({this.noteSelectedFunction, this.notes}); JournalList({
@required this.notes,
@required this.noteSelectedFunction,
@required this.noteRemover,
});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -22,7 +28,20 @@ class JournalList extends StatelessWidget {
return null; return null;
} }
//if (i.isOdd) return new Divider(); //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")));
},
);
}, },
); );
} }

View File

@ -9,10 +9,12 @@ import 'package:journal/note_viewer.dart';
class HomeScreen extends StatelessWidget { class HomeScreen extends StatelessWidget {
final AppState appState; final AppState appState;
final NoteAdder noteAdder; final NoteAdder noteAdder;
final NoteRemover noteRemover;
HomeScreen({ HomeScreen({
@required this.appState, @required this.appState,
@required this.noteAdder, @required this.noteAdder,
@required this.noteRemover,
}); });
@override @override
@ -47,6 +49,7 @@ class HomeScreen extends StatelessWidget {
body: new JournalList( body: new JournalList(
notes: appState.notes, notes: appState.notes,
noteSelectedFunction: (note) => _noteSelected(note, context), noteSelectedFunction: (note) => _noteSelected(note, context),
noteRemover: noteRemover,
), ),
); );
} }