diff --git a/lib/screens/note_list.dart b/lib/screens/note_list.dart index a476100..94e744b 100644 --- a/lib/screens/note_list.dart +++ b/lib/screens/note_list.dart @@ -1,170 +1,145 @@ -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_app/screens/note_detail.dart'; +import 'package:flutter_apps/models/note.dart'; +import 'package:flutter_apps/utils/database_helper.dart'; import 'package:sqflite/sqflite.dart'; +import 'note_detail.dart'; class NoteList extends StatefulWidget { - - @override - State createState() { - - return NoteListState(); - } + @override + _NoteListState createState() => _NoteListState(); } -class NoteListState extends State { +class _NoteListState extends State { + int count = 0; + DatabaseHelper databaseHelper = DatabaseHelper(); + List? noteList; - DatabaseHelper databaseHelper = DatabaseHelper(); - List noteList; - int count = 0; - - @override + @override Widget build(BuildContext context) { - - if (noteList == null) { - noteList = List(); - updateListView(); - } + if (noteList == null) { + noteList = []; + updateListView(); + print("Build"); + } return Scaffold( - - appBar: AppBar( - title: Text('Notes'), - ), - - body: getNoteListView(), - - floatingActionButton: FloatingActionButton( - onPressed: () { - debugPrint('FAB clicked'); - navigateToDetail(Note('', '', 2), 'Add Note'); - }, - - tooltip: 'Add Note', - - child: Icon(Icons.add), - - ), + appBar: AppBar( + title: Text('Notes'), + ), + body: getNoteListView(), + floatingActionButton: FloatingActionButton( + onPressed: () { + print("FAB clicked"); + navigateToDetail(Note('', '', 2), 'Add Note'); + }, + tooltip: 'Add Note', + child: Icon(Icons.add), + ), ); } ListView getNoteListView() { - - TextStyle titleStyle = Theme.of(context).textTheme.subhead; - - return ListView.builder( - itemCount: count, - itemBuilder: (BuildContext context, int position) { - return Card( - color: Colors.white, - elevation: 2.0, - child: ListTile( - - leading: CircleAvatar( - backgroundColor: getPriorityColor(this.noteList[position].priority), - child: getPriorityIcon(this.noteList[position].priority), - ), - - title: Text(this.noteList[position].title, style: titleStyle,), - - subtitle: Text(this.noteList[position].date), - - trailing: GestureDetector( - child: Icon(Icons.delete, color: Colors.grey,), - onTap: () { - _delete(context, noteList[position]); - }, - ), - - - onTap: () { - debugPrint("ListTile Tapped"); - navigateToDetail(this.noteList[position],'Edit Note'); - }, - - ), - ); - }, - ); + TextStyle? titleStyle = Theme.of(context).textTheme.subtitle1; + + return ListView.builder( + itemCount: count, + itemBuilder: (BuildContext context, int position) { + return Card( + color: Colors.white, + elevation: 2.0, + child: ListTile( + leading: CircleAvatar( + backgroundColor: + getPriorityColor(this.noteList![position].priority!), + child: getPriorityIcon(this.noteList![position].priority!), + ), + title: Text( + this.noteList![position].title!, + style: titleStyle, + ), + subtitle: Text(this.noteList![position].date!), + trailing: GestureDetector( + child: Icon( + Icons.delete, + color: Colors.grey, + ), + onTap: () { + _delete(context, noteList![position]); + }, + ), + onTap: () { + print("ListTile Tapped"); + navigateToDetail(this.noteList![position], 'Edit Note'); + }, + ), + ); + }, + ); } - // Returns the priority color - Color getPriorityColor(int priority) { - switch (priority) { - case 1: - return Colors.red; - break; - case 2: - return Colors.yellow; - break; - - default: - return Colors.yellow; - } - } - - // Returns the priority icon - Icon getPriorityIcon(int priority) { - switch (priority) { - case 1: - return Icon(Icons.play_arrow); - break; - case 2: - return Icon(Icons.keyboard_arrow_right); - break; - - default: - return Icon(Icons.keyboard_arrow_right); - } - } - - void _delete(BuildContext context, Note note) async { + // Colors according to the priority + Color getPriorityColor(int priority) { + switch (priority) { + case 1: + return Colors.red; + case 2: + return Colors.yellow; + default: + return Colors.yellow; + } + } - int result = await databaseHelper.deleteNote(note.id); - if (result != 0) { - _showSnackBar(context, 'Note Deleted Successfully'); - updateListView(); - } - } + // Icon according to the priority + Icon getPriorityIcon(int priority) { + switch (priority) { + case 1: + return Icon(Icons.play_arrow); + case 2: + return Icon(Icons.keyboard_arrow_right); + default: + return Icon(Icons.keyboard_arrow_right); + } + } - void _showSnackBar(BuildContext context, String message) { + void _delete(BuildContext context, Note note) async { + int result = await databaseHelper.deleteNote(note.id!); + if (result != 0) { + _showSnackBar(context, 'Note Deleted Successfully!'); + updateListView(); + } + } - final snackBar = SnackBar(content: Text(message)); - Scaffold.of(context).showSnackBar(snackBar); - } + void _showSnackBar(BuildContext context, String message) { + final snackBar = SnackBar(content: Text(message)); + ScaffoldMessenger.of(context).showSnackBar(snackBar); + } void navigateToDetail(Note note, String title) async { - bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) { - return NoteDetail(note, title); - })); + bool result = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) { + return NoteDetail(note, title); + }, + ), + ); - if (result == true) { - updateListView(); - } + if (result == true) { + updateListView(); + } } void updateListView() { - - final Future dbFuture = databaseHelper.initializeDatabase(); - dbFuture.then((database) { - - Future> noteListFuture = databaseHelper.getNoteList(); - noteListFuture.then((noteList) { - setState(() { - this.noteList = noteList; - this.count = noteList.length; - }); - }); - }); + final Future dbFuture = databaseHelper.initializeDatabase(); + dbFuture.then((database) { + Future> noteListFuture = databaseHelper.getNoteList(); + noteListFuture.then((noteList) { + setState(() { + this.noteList = noteList; + this.count = noteList.length; + }); + }); + }); } } - - - - - - -