diff --git a/lib/screens/note_detail.dart b/lib/screens/note_detail.dart index 6192dc0..2e8a444 100644 --- a/lib/screens/note_detail.dart +++ b/lib/screens/note_detail.dart @@ -1,285 +1,224 @@ -import 'dart:async'; import 'package:flutter/material.dart'; -import 'package:flutter_app/models/note.dart'; -import 'package:flutter_app/utils/database_helper.dart'; +import 'package:flutter_codes/models/note.dart'; +import 'package:flutter_codes/utils/database_helper.dart'; import 'package:intl/intl.dart'; class NoteDetail extends StatefulWidget { - - final String appBarTitle; - final Note note; - - NoteDetail(this. note, this.appBarTitle); - - @override - State createState() { - - return NoteDetailState(this.note, this.appBarTitle); - } + final String appBarTitle; + final Note note; + NoteDetail(this.note, this.appBarTitle); + @override + NoteDetailState createState() => NoteDetailState(this.note, this.appBarTitle); } class NoteDetailState extends State { + static var _priorities = ['High', 'Low']; - static var _priorities = ['High', 'Low']; - - DatabaseHelper helper = DatabaseHelper(); + final String appBarTitle; + final Note note; - String appBarTitle; - Note note; + DatabaseHelper helper = DatabaseHelper(); - TextEditingController titleController = TextEditingController(); - TextEditingController descriptionController = TextEditingController(); + NoteDetailState(this.note, this.appBarTitle); - NoteDetailState(this.note, this.appBarTitle); + TextEditingController titleController = TextEditingController(); + TextEditingController descriptionController = TextEditingController(); - @override + @override Widget build(BuildContext context) { + TextStyle? textStyle = Theme.of(context).textTheme.headline6; - TextStyle textStyle = Theme.of(context).textTheme.title; + // if (note.title != null) { + // titleController.text = note.title!; + // } + // if (note.description != null) { + // descriptionController.text = note.description!; + // } - titleController.text = note.title; - descriptionController.text = note.description; + titleController.text = note.title ?? ''; + descriptionController.text = note.description ?? ''; return WillPopScope( - - onWillPop: () { - // Write some code to control things, when user press Back navigation button in device navigationBar - moveToLastScreen(); - }, - - child: Scaffold( - appBar: AppBar( - title: Text(appBarTitle), - leading: IconButton(icon: Icon( - Icons.arrow_back), - onPressed: () { - // Write some code to control things, when user press back button in AppBar - moveToLastScreen(); - } - ), - ), - - body: Padding( - padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0), - child: ListView( - children: [ - - // First element - ListTile( - title: DropdownButton( - items: _priorities.map((String dropDownStringItem) { - return DropdownMenuItem ( - value: dropDownStringItem, - child: Text(dropDownStringItem), - ); - }).toList(), - - style: textStyle, - - value: getPriorityAsString(note.priority), - - onChanged: (valueSelectedByUser) { - setState(() { - debugPrint('User selected $valueSelectedByUser'); - updatePriorityAsInt(valueSelectedByUser); - }); - } - ), - ), - - // Second Element - Padding( - padding: EdgeInsets.only(top: 15.0, bottom: 15.0), - child: TextField( - controller: titleController, - style: textStyle, - onChanged: (value) { - debugPrint('Something changed in Title Text Field'); - updateTitle(); - }, - decoration: InputDecoration( - labelText: 'Title', - labelStyle: textStyle, - border: OutlineInputBorder( - borderRadius: BorderRadius.circular(5.0) - ) - ), - ), - ), - - // Third Element - Padding( - padding: EdgeInsets.only(top: 15.0, bottom: 15.0), - child: TextField( - controller: descriptionController, - style: textStyle, - onChanged: (value) { - debugPrint('Something changed in Description Text Field'); - updateDescription(); - }, - decoration: InputDecoration( - labelText: 'Description', - labelStyle: textStyle, - border: OutlineInputBorder( - borderRadius: BorderRadius.circular(5.0) - ) - ), - ), - ), - - // Fourth Element - Padding( - padding: EdgeInsets.only(top: 15.0, bottom: 15.0), - child: Row( - children: [ - Expanded( - child: RaisedButton( - color: Theme.of(context).primaryColorDark, - textColor: Theme.of(context).primaryColorLight, - child: Text( - 'Save', - textScaleFactor: 1.5, - ), - onPressed: () { - setState(() { - debugPrint("Save button clicked"); - _save(); - }); - }, - ), - ), - - Container(width: 5.0,), - - Expanded( - child: RaisedButton( - color: Theme.of(context).primaryColorDark, - textColor: Theme.of(context).primaryColorLight, - child: Text( - 'Delete', - textScaleFactor: 1.5, - ), - onPressed: () { - setState(() { - debugPrint("Delete button clicked"); - _delete(); - }); - }, - ), - ), - - ], - ), - ), - - ], - ), - ), - - )); + onWillPop: () async { + moveToLastScreen(); + return true; + }, + child: Scaffold( + appBar: AppBar( + title: Text(appBarTitle), + leading: IconButton( + icon: Icon(Icons.arrow_back), + onPressed: () { + print("back"); + moveToLastScreen(); + }, + ), + ), + body: Padding( + padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0), + child: ListView( + children: [ + ListTile( + title: DropdownButton( + items: _priorities.map((String dropDownStringItem) { + return DropdownMenuItem( + value: dropDownStringItem, + child: Text(dropDownStringItem), + ); + }).toList(), + value: getPriorityAsString(note.priority!), + onChanged: (valueSelectedByUser) { + setState(() { + print('User selected $valueSelectedByUser'); + updatePriorityAsInt(valueSelectedByUser.toString()); + }); + }, + ), + ), + Padding( + padding: EdgeInsets.fromLTRB(0, 15.0, 0.0, 15.0), + child: TextField( + controller: titleController, + style: textStyle, + onChanged: (value) { + print('Something changed in Title Text Field'); + updateTitle(); + }, + decoration: InputDecoration( + labelText: 'Title', + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(5.0), + ), + ), + ), + ), + Padding( + padding: EdgeInsets.fromLTRB(0, 15.0, 0.0, 15.0), + child: TextField( + controller: descriptionController, + style: textStyle, + onChanged: (value) { + print('Something changed in Description Text Field'); + updateDescription(); + }, + decoration: InputDecoration( + labelText: 'Description', + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(5.0), + ), + ), + ), + ), + Padding( + padding: EdgeInsets.fromLTRB(0.0, 15.0, 0, 15.0), + child: Row( + children: [ + Expanded( + child: ElevatedButton( + child: Text("Save", textScaleFactor: 1.5), + onPressed: () { + setState(() { + _save(); + }); + }, + ), + ), + SizedBox( + width: 5.0, + ), + Expanded( + child: ElevatedButton( + child: Text("Delete", textScaleFactor: 1.5), + onPressed: () { + setState(() { + _delete(); + }); + }, + ), + ), + ], + ), + ), + ], + ), + ), + ), + ); } void moveToLastScreen() { - Navigator.pop(context, true); + Navigator.pop(context, true); } - // Convert the String priority in the form of integer before saving it to Database - void updatePriorityAsInt(String value) { - switch (value) { - case 'High': - note.priority = 1; - break; - case 'Low': - note.priority = 2; - break; - } - } - - // Convert int priority to String priority and display it to user in DropDown - String getPriorityAsString(int value) { - String priority; - switch (value) { - case 1: - priority = _priorities[0]; // 'High' - break; - case 2: - priority = _priorities[1]; // 'Low' - break; - } - return priority; - } + void updatePriorityAsInt(String? value) { + switch (value) { + case 'High': + note.priority = 1; + break; + case 'Low': + note.priority = 2; + break; + } + } - // Update the title of Note object - void updateTitle(){ + void updateTitle() { note.title = titleController.text; } - // Update the description of Note object - void updateDescription() { - note.description = descriptionController.text; - } - - // Save data to database - void _save() async { - - moveToLastScreen(); - - note.date = DateFormat.yMMMd().format(DateTime.now()); - int result; - if (note.id != null) { // Case 1: Update operation - result = await helper.updateNote(note); - } else { // Case 2: Insert Operation - result = await helper.insertNote(note); - } - - if (result != 0) { // Success - _showAlertDialog('Status', 'Note Saved Successfully'); - } else { // Failure - _showAlertDialog('Status', 'Problem Saving Note'); - } - - } - - void _delete() async { - - moveToLastScreen(); - - // Case 1: If user is trying to delete the NEW NOTE i.e. he has come to - // the detail page by pressing the FAB of NoteList page. - if (note.id == null) { - _showAlertDialog('Status', 'No Note was deleted'); - return; - } + void updateDescription() { + note.description = descriptionController.text; + } - // Case 2: User is trying to delete the old note that already has a valid ID. - int result = await helper.deleteNote(note.id); - if (result != 0) { - _showAlertDialog('Status', 'Note Deleted Successfully'); - } else { - _showAlertDialog('Status', 'Error Occured while Deleting Note'); - } - } + void _save() async { + moveToLastScreen(); + note.date = DateFormat.yMMMd().format(DateTime.now()); + int result; + if (note.id != null) { + result = await helper.updateNote(note); + } else { + result = await helper.insertNote(note); + } + + if (result != 0) { + _showAlertDialog('Status', "Note Saved Successfully!"); + } else { + _showAlertDialog('Status', "Problem Saving Note"); + } + } - void _showAlertDialog(String title, String message) { + void _delete() async { + moveToLastScreen(); + if (note.id == null) { + _showAlertDialog('Status', 'No note was deleted!'); + return; + } + int result = await helper.deleteNote(note.id!); + if (result != 0) { + _showAlertDialog('Status', 'Note Deleted Successfully'); + } else { + _showAlertDialog('Status', 'Error Occured'); + } + } - AlertDialog alertDialog = AlertDialog( - title: Text(title), - content: Text(message), - ); - showDialog( - context: context, - builder: (_) => alertDialog - ); - } + void _showAlertDialog(String title, String message) { + AlertDialog alertDialog = + AlertDialog(title: Text(title), content: Text(message)); + showDialog( + context: context, + builder: (_) => alertDialog, + ); + } + String? getPriorityAsString(int value) { + String? priority; + switch (value) { + case 1: + priority = _priorities[0]; + break; + case 2: + priority = _priorities[1]; + break; + } + return priority; + } } - - - - - - - - - -