Skip to content

Commit 5f2266b

Browse files
committed
add update, delet,get
1 parent 6707401 commit 5f2266b

File tree

6 files changed

+177
-18
lines changed

6 files changed

+177
-18
lines changed

lib/data/firestor.dart

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:cloud_firestore/cloud_firestore.dart';
22
import 'package:firebase_auth/firebase_auth.dart';
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_to_do_list/model/notes_model.dart';
35
import 'package:uuid/uuid.dart';
46

57
class Firestore_Datasource {
@@ -14,6 +16,7 @@ class Firestore_Datasource {
1416
.set({"id": _auth.currentUser!.uid, "email": email});
1517
return true;
1618
} catch (e) {
19+
print(e);
1720
return true;
1821
}
1922
}
@@ -32,11 +35,92 @@ class Firestore_Datasource {
3235
'subtitle': subtitle,
3336
'isDon': false,
3437
'image': image,
35-
'time': data,
38+
'time': '${data.hour}:${data.minute}',
3639
'title': title,
3740
});
3841
return true;
3942
} catch (e) {
43+
print(e);
44+
return true;
45+
}
46+
}
47+
48+
List getNotes(AsyncSnapshot snapshot) {
49+
try {
50+
final notesList = snapshot.data!.docs.map((doc) {
51+
final data = doc.data() as Map<String, dynamic>;
52+
return Note(
53+
data['id'],
54+
data['subtitle'],
55+
data['time'],
56+
data['image'],
57+
data['title'],
58+
data['isDon'],
59+
);
60+
}).toList();
61+
return notesList;
62+
} catch (e) {
63+
print(e);
64+
return [];
65+
}
66+
}
67+
68+
Stream<QuerySnapshot> stream() {
69+
return _firestore
70+
.collection('users')
71+
.doc(_auth.currentUser!.uid)
72+
.collection('notes')
73+
.snapshots();
74+
}
75+
76+
Future<bool> isdone(String uuid, bool isDon) async {
77+
try {
78+
await _firestore
79+
.collection('users')
80+
.doc(_auth.currentUser!.uid)
81+
.collection('notes')
82+
.doc(uuid)
83+
.update({'isDon': isDon});
84+
return true;
85+
} catch (e) {
86+
print(e);
87+
return true;
88+
}
89+
}
90+
91+
Future<bool> Update_Note(
92+
String uuid, int image, String title, String subtitle) async {
93+
try {
94+
DateTime data = new DateTime.now();
95+
await _firestore
96+
.collection('users')
97+
.doc(_auth.currentUser!.uid)
98+
.collection('notes')
99+
.doc(uuid)
100+
.update({
101+
'time': '${data.hour}:${data.minute}',
102+
'subtitle': subtitle,
103+
'title': title,
104+
'image': image,
105+
});
106+
return true;
107+
} catch (e) {
108+
print(e);
109+
return true;
110+
}
111+
}
112+
113+
Future<bool> delet_note(String uuid) async {
114+
try {
115+
await _firestore
116+
.collection('users')
117+
.doc(_auth.currentUser!.uid)
118+
.collection('notes')
119+
.doc(uuid)
120+
.delete();
121+
return true;
122+
} catch (e) {
123+
print(e);
40124
return true;
41125
}
42126
}

lib/model/notes_model.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Note {
2+
String id;
3+
String subtitle;
4+
String title;
5+
String time;
6+
int image;
7+
bool isDon;
8+
Note(this.id, this.subtitle, this.time, this.image, this.title, this.isDon);
9+
}

lib/screen/edit_screen.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_to_do_list/const/colors.dart';
3+
import 'package:flutter_to_do_list/data/firestor.dart';
4+
import 'package:flutter_to_do_list/model/notes_model.dart';
35

46
class Edit_Screen extends StatefulWidget {
5-
const Edit_Screen({super.key});
7+
Note _note;
8+
Edit_Screen(this._note, {super.key});
69

710
@override
811
State<Edit_Screen> createState() => _Edit_ScreenState();
912
}
1013

1114
class _Edit_ScreenState extends State<Edit_Screen> {
12-
final title = TextEditingController();
13-
final subtitle = TextEditingController();
15+
TextEditingController? title;
16+
TextEditingController? subtitle;
1417

1518
FocusNode _focusNode1 = FocusNode();
1619
FocusNode _focusNode2 = FocusNode();
1720
int indexx = 0;
1821
@override
22+
void initState() {
23+
// TODO: implement initState
24+
super.initState();
25+
title = TextEditingController(text: widget._note.title);
26+
subtitle = TextEditingController(text: widget._note.subtitle);
27+
}
28+
1929
Widget build(BuildContext context) {
2030
return Scaffold(
2131
backgroundColor: backgroundColors,
@@ -46,6 +56,8 @@ class _Edit_ScreenState extends State<Edit_Screen> {
4656
minimumSize: Size(170, 48),
4757
),
4858
onPressed: () {
59+
Firestore_Datasource().Update_Note(
60+
widget._note.id, indexx, title!.text, subtitle!.text);
4961
Navigator.pop(context);
5062
},
5163
child: Text('add task'),

lib/screen/home.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
12
import 'package:firebase_auth/firebase_auth.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter/rendering.dart';
45
import 'package:flutter_to_do_list/const/colors.dart';
6+
import 'package:flutter_to_do_list/data/firestor.dart';
57
import 'package:flutter_to_do_list/screen/add_note_screen.dart';
68
import 'package:flutter_to_do_list/widgets/task_widgets.dart';
79

@@ -46,12 +48,26 @@ class _Home_ScreenState extends State<Home_Screen> {
4648
}
4749
return true;
4850
},
49-
child: ListView.builder(
50-
itemBuilder: (context, index) {
51-
return Task_Widget();
52-
},
53-
itemCount: 10,
54-
),
51+
child: StreamBuilder<QuerySnapshot>(
52+
stream: Firestore_Datasource().stream(),
53+
builder: (context, snapshot) {
54+
if (!snapshot.hasData) {
55+
return CircularProgressIndicator();
56+
}
57+
final noteslist = Firestore_Datasource().getNotes(snapshot);
58+
return ListView.builder(
59+
itemBuilder: (context, index) {
60+
final note = noteslist[index];
61+
return Dismissible(
62+
key: UniqueKey(),
63+
onDismissed: (direction) {
64+
Firestore_Datasource().delet_note(note.id);
65+
},
66+
child: Task_Widget(note));
67+
},
68+
itemCount: noteslist.length,
69+
);
70+
}),
5571
)),
5672
);
5773
}

lib/widgets/stream_note.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_to_do_list/widgets/task_widgets.dart';
4+
5+
import '../data/firestor.dart';
6+
7+
class Stream_note extends StatelessWidget {
8+
const Stream_note({super.key});
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return StreamBuilder<QuerySnapshot>(
13+
stream: Firestore_Datasource().stream(),
14+
builder: (context, snapshot) {
15+
if (!snapshot.hasData) {
16+
return CircularProgressIndicator();
17+
}
18+
final noteslist = Firestore_Datasource().getNotes(snapshot);
19+
return ListView.builder(
20+
shrinkWrap: true,
21+
itemBuilder: (context, index) {
22+
final note = noteslist[index];
23+
return Dismissible(
24+
key: UniqueKey(),
25+
onDismissed: (direction) {
26+
Firestore_Datasource().delet_note(note.id);
27+
},
28+
child: Task_Widget(note));
29+
},
30+
itemCount: noteslist.length,
31+
);
32+
});
33+
}
34+
}

lib/widgets/task_widgets.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_to_do_list/const/colors.dart';
3+
import 'package:flutter_to_do_list/data/firestor.dart';
4+
import 'package:flutter_to_do_list/model/notes_model.dart';
35
import 'package:flutter_to_do_list/screen/edit_screen.dart';
46

57
class Task_Widget extends StatefulWidget {
6-
const Task_Widget({super.key});
8+
Note _note;
9+
Task_Widget(this._note, {super.key});
710

811
@override
912
State<Task_Widget> createState() => _Task_WidgetState();
1013
}
1114

12-
bool isDone = false;
13-
1415
class _Task_WidgetState extends State<Task_Widget> {
1516
@override
1617
Widget build(BuildContext context) {
18+
bool isDone = widget._note.isDon;
1719
return Padding(
1820
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
1921
child: Container(
@@ -48,7 +50,7 @@ class _Task_WidgetState extends State<Task_Widget> {
4850
mainAxisAlignment: MainAxisAlignment.spaceBetween,
4951
children: [
5052
Text(
51-
'title',
53+
widget._note.title,
5254
style: TextStyle(
5355
fontSize: 18,
5456
fontWeight: FontWeight.bold,
@@ -61,12 +63,14 @@ class _Task_WidgetState extends State<Task_Widget> {
6163
setState(() {
6264
isDone = !isDone;
6365
});
66+
Firestore_Datasource()
67+
.isdone(widget._note.id, isDone);
6468
},
6569
)
6670
],
6771
),
6872
Text(
69-
'subtitle',
73+
widget._note.subtitle,
7074
style: TextStyle(
7175
fontSize: 16,
7276
fontWeight: FontWeight.w400,
@@ -106,7 +110,7 @@ class _Task_WidgetState extends State<Task_Widget> {
106110
Image.asset('images/icon_time.png'),
107111
SizedBox(width: 10),
108112
Text(
109-
'time',
113+
widget._note.time,
110114
style: TextStyle(
111115
color: Colors.white,
112116
fontSize: 14,
@@ -121,7 +125,7 @@ class _Task_WidgetState extends State<Task_Widget> {
121125
GestureDetector(
122126
onTap: () {
123127
Navigator.of(context).push(MaterialPageRoute(
124-
builder: (context) => Edit_Screen(),
128+
builder: (context) => Edit_Screen(widget._note),
125129
));
126130
},
127131
child: Container(
@@ -164,7 +168,7 @@ class _Task_WidgetState extends State<Task_Widget> {
164168
decoration: BoxDecoration(
165169
color: Colors.white,
166170
image: DecorationImage(
167-
image: AssetImage('images/1.png'),
171+
image: AssetImage('images/${widget._note.image}.png'),
168172
fit: BoxFit.cover,
169173
),
170174
),

0 commit comments

Comments
 (0)