diff --git a/lib/note_editor.dart b/lib/note_editor.dart index 6d3bd862..79c5ea5c 100644 --- a/lib/note_editor.dart +++ b/lib/note_editor.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:journal/note.dart'; @@ -15,13 +17,43 @@ class NoteEditor extends StatelessWidget { final container = StateContainer.of(context); var bodyWidget = new Container( - child: new TextFormField( - key: noteTextKey, - autofocus: true, - keyboardType: TextInputType.multiline, - maxLines: 5000, - decoration: new InputDecoration( - hintText: 'Write here', + child: new Form( + // Show a dialog if discarding non-empty notes + onWillPop: () { + return Future(() { + var noteContent = noteTextKey.currentState.value.trim(); + if (noteContent.isEmpty) { + return true; + } + return showDialog( + context: context, + builder: (BuildContext context) { + return new AlertDialog( + title: new Text('Are you sure?'), + content: new Text('Do you want to discard the entry'), + actions: [ + new FlatButton( + onPressed: () => Navigator.of(context).pop(false), + child: new Text('No'), + ), + new FlatButton( + onPressed: () => Navigator.of(context).pop(true), + child: new Text('Yes'), + ), + ], + ); + }, + ); + }); + }, + child: TextFormField( + key: noteTextKey, + autofocus: true, + keyboardType: TextInputType.multiline, + maxLines: 5000, + decoration: new InputDecoration( + hintText: 'Write here', + ), ), ), padding: const EdgeInsets.all(8.0), @@ -38,10 +70,10 @@ class NoteEditor extends StatelessWidget { floatingActionButton: FloatingActionButton( child: Icon(Icons.add), onPressed: () { - var body = noteTextKey.currentState.value; + var noteContent = noteTextKey.currentState.value; var note = new Note( createdAt: _createdAt, - body: body, + body: noteContent, ); container.addNote(note);