forked from mdanics/fluttergram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomment_screen.dart
186 lines (166 loc) · 4.77 KB
/
comment_screen.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import "dart:async";
import "main.dart"; //for current user
class CommentScreen extends StatefulWidget {
final String postId;
final String postOwner;
final String postMediaUrl;
const CommentScreen({this.postId, this.postOwner, this.postMediaUrl});
@override
_CommentScreenState createState() => _CommentScreenState(
postId: this.postId,
postOwner: this.postOwner,
postMediaUrl: this.postMediaUrl);
}
class _CommentScreenState extends State<CommentScreen> {
final String postId;
final String postOwner;
final String postMediaUrl;
bool didFetchComments = false;
List<Comment> fetchedComments = [];
final TextEditingController _commentController = TextEditingController();
_CommentScreenState({this.postId, this.postOwner, this.postMediaUrl});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Comments",
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.white,
),
body: buildPage(),
);
}
Widget buildPage() {
return Column(
children: [
Expanded(
child:
buildComments(),
),
Divider(),
ListTile(
title: TextFormField(
controller: _commentController,
decoration: InputDecoration(labelText: 'Write a comment...'),
onFieldSubmitted: addComment,
),
trailing: OutlineButton(onPressed: (){addComment(_commentController.text);}, borderSide: BorderSide.none, child: Text("Post"),),
),
],
);
}
Widget buildComments() {
if (this.didFetchComments == false){
return FutureBuilder<List<Comment>>(
future: getComments(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Container(
alignment: FractionalOffset.center,
child: CircularProgressIndicator());
this.didFetchComments = true;
this.fetchedComments = snapshot.data;
return ListView(
children: snapshot.data,
);
});
} else {
// for optimistic updating
return ListView(
children: this.fetchedComments
);
}
}
Future<List<Comment>> getComments() async {
List<Comment> comments = [];
QuerySnapshot data = await FirebaseFirestore.instance
.collection("insta_comments")
.doc(postId)
.collection("comments")
.get();
data.docs.forEach((DocumentSnapshot doc) {
comments.add(Comment.fromDocument(doc));
});
return comments;
}
addComment(String comment) {
_commentController.clear();
FirebaseFirestore.instance
.collection("insta_comments")
.doc(postId)
.collection("comments")
.add({
"username": currentUserModel.username,
"comment": comment,
"timestamp": Timestamp.now(),
"avatarUrl": currentUserModel.photoUrl,
"userId": currentUserModel.id
});
//adds to postOwner's activity feed
FirebaseFirestore.instance
.collection("insta_a_feed")
.doc(postOwner)
.collection("items")
.add({
"username": currentUserModel.username,
"userId": currentUserModel.id,
"type": "comment",
"userProfileImg": currentUserModel.photoUrl,
"commentData": comment,
"timestamp": Timestamp.now(),
"postId": postId,
"mediaUrl": postMediaUrl,
});
// add comment to the current listview for an optimistic update
setState(() {
fetchedComments = List.from(fetchedComments)..add(Comment(
username: currentUserModel.username,
comment: comment,
timestamp: Timestamp.now(),
avatarUrl: currentUserModel.photoUrl,
userId: currentUserModel.id
));
});
}
}
class Comment extends StatelessWidget {
final String username;
final String userId;
final String avatarUrl;
final String comment;
final Timestamp timestamp;
Comment(
{this.username,
this.userId,
this.avatarUrl,
this.comment,
this.timestamp});
factory Comment.fromDocument(DocumentSnapshot document) {
var data = document.data();
return Comment(
username: data['username'],
userId: data['userId'],
comment: data["comment"],
timestamp: data["timestamp"],
avatarUrl: data["avatarUrl"],
);
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
ListTile(
title: Text(comment),
leading: CircleAvatar(
backgroundImage: NetworkImage(avatarUrl),
),
),
Divider(),
],
);
}
}