From 3c9a773e186bfa992f246133a922706a3dbf053a Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 16 Feb 2019 18:29:44 +0100 Subject: [PATCH] NoteViewer: Show a confirmation dialog before deleting --- lib/screens/note_viewer.dart | 47 +++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/screens/note_viewer.dart b/lib/screens/note_viewer.dart index 0cdfcbce..1c739f86 100644 --- a/lib/screens/note_viewer.dart +++ b/lib/screens/note_viewer.dart @@ -48,17 +48,7 @@ class NoteBrowsingScreenState extends State { IconButton( icon: Icon(Icons.delete), onPressed: () { - final stateContainer = StateContainer.of(context); - var noteIndex = _currentIndex(); - Note note = widget.notes[noteIndex]; - stateContainer.removeNote(note); - Navigator.pop(context); - - print("Shwoing an undo snackbar"); - var snackbar = buildUndoDeleteSnackbar(context, note, noteIndex); - _scaffoldKey.currentState - ..removeCurrentSnackBar() - ..showSnackBar(snackbar); + showDialog(context: context, builder: _buildAlertDialog); }, ), ], @@ -83,6 +73,41 @@ class NoteBrowsingScreenState extends State { assert(currentIndex < widget.notes.length); return currentIndex; } + + void _deleteNote(BuildContext context) { + final stateContainer = StateContainer.of(context); + var noteIndex = _currentIndex(); + Note note = widget.notes[noteIndex]; + stateContainer.removeNote(note); + Navigator.pop(context); + + print("Shwoing an undo snackbar"); + var snackbar = buildUndoDeleteSnackbar(context, note, noteIndex); + _scaffoldKey.currentState + ..removeCurrentSnackBar() + ..showSnackBar(snackbar); + } + + Widget _buildAlertDialog(BuildContext context) { + var title = "Are you sure you want to delete this Journal Entry?"; + + return AlertDialog( + content: Text(title), + actions: [ + FlatButton( + onPressed: () => Navigator.pop(context), + child: Text('Keep It'), + ), + FlatButton( + onPressed: () { + Navigator.pop(context); // Alert box + _deleteNote(context); + }, + child: Text('Delete It'), + ), + ], + ); + } } class NoteViewer extends StatelessWidget {