-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathnote_list.dart
172 lines (127 loc) · 3.41 KB
/
note_list.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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:sqflite/sqflite.dart';
class NoteList extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return NoteListState();
}
}
class NoteListState extends State<NoteList> {
DatabaseHelper databaseHelper = DatabaseHelper();
List<Note> noteList;
int count = 0;
@override
Widget build(BuildContext context) {
if (noteList == null) {
noteList = List<Note>();
updateListView();
}
return Scaffold(
appBar: AppBar(
title: Text('Notes'),
centerTitle: false,
titleSpacing: 0.0,
),
body: getNoteListView(),
floatingActionButton: FloatingActionButton(
onPressed: () {
debugPrint('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');
},
),
);
},
);
}
// 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 {
int result = await databaseHelper.deleteNote(note.id);
if (result != 0) {
_showSnackBar(context, '1 Note Deleted Successfully');
updateListView();
}
}
void _showSnackBar(BuildContext context, String message) {
final snackBar = SnackBar(content: Text(message));
Scaffold.of(context).showSnackBar(snackBar);
}
void navigateToDetail(Note note, String title) async {
bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) {
return NoteDetail(note, title);
}));
if (result == true) {
updateListView();
}
}
void updateListView() {
final Future<Database> dbFuture = databaseHelper.initializeDatabase();
dbFuture.then((database) {
Future<List<Note>> noteListFuture = databaseHelper.getNoteList();
noteListFuture.then((noteList) {
setState(() {
this.noteList = noteList;
this.count = noteList.length;
});
});
});
}
}