forked from mdanics/fluttergram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivity_feed.dart
207 lines (191 loc) · 5.56 KB
/
activity_feed.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'image_post.dart'; //needed to open image when clicked
import 'profile_page.dart'; // to open the profile page when username clicked
import 'main.dart'; //needed for currentuser id
class ActivityFeedPage extends StatefulWidget {
@override
_ActivityFeedPageState createState() => _ActivityFeedPageState();
}
class _ActivityFeedPageState extends State<ActivityFeedPage> with AutomaticKeepAliveClientMixin<ActivityFeedPage> {
@override
Widget build(BuildContext context) {
super.build(context); // reloads state when opened again
return Scaffold(
appBar: AppBar(
title: Text(
"Activity Feed",
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.white,
),
body: buildActivityFeed(),
);
}
buildActivityFeed() {
return Container(
child: FutureBuilder(
future: getFeed(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Container(
alignment: FractionalOffset.center,
padding: const EdgeInsets.only(top: 10.0),
child: CircularProgressIndicator());
else {
return ListView(children: snapshot.data);
}
}),
);
}
getFeed() async {
List<ActivityFeedItem> items = [];
var snap = await FirebaseFirestore.instance
.collection('insta_a_feed')
.doc(currentUserModel.id)
.collection("items")
.orderBy("timestamp")
.get();
for (var doc in snap.docs) {
items.add(ActivityFeedItem.fromDocument(doc));
}
return items;
}
// ensures state is kept when switching pages
@override
bool get wantKeepAlive => true;
}
class ActivityFeedItem extends StatelessWidget {
final String username;
final String userId;
final String
type; // types include liked photo, follow user, comment on photo
final String mediaUrl;
final String mediaId;
final String userProfileImg;
final String commentData;
ActivityFeedItem(
{this.username,
this.userId,
this.type,
this.mediaUrl,
this.mediaId,
this.userProfileImg,
this.commentData});
factory ActivityFeedItem.fromDocument(DocumentSnapshot document) {
var data = document.data();
return ActivityFeedItem(
username: data['username'],
userId: data['userId'],
type: data['type'],
mediaUrl: data['mediaUrl'],
mediaId: data['postId'],
userProfileImg: data['userProfileImg'],
commentData: data["commentData"],
);
}
Widget mediaPreview = Container();
String actionText;
void configureItem(BuildContext context) {
if (type == "like" || type == "comment") {
mediaPreview = GestureDetector(
onTap: () {
openImage(context, mediaId);
},
child: Container(
height: 45.0,
width: 45.0,
child: AspectRatio(
aspectRatio: 487 / 451,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
alignment: FractionalOffset.topCenter,
image: NetworkImage(mediaUrl),
)),
),
),
),
);
}
if (type == "like") {
actionText = " liked your post.";
} else if (type == "follow") {
actionText = " starting following you.";
} else if (type == "comment") {
actionText = " commented: $commentData";
} else {
actionText = "Error - invalid activityFeed type: $type";
}
}
@override
Widget build(BuildContext context) {
configureItem(context);
return Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 15.0),
child: CircleAvatar(
radius: 23.0,
backgroundImage: NetworkImage(userProfileImg),
),
),
Expanded(
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
child: Text(
username,
style: TextStyle(fontWeight: FontWeight.bold),
),
onTap: () {
openProfile(context, userId);
},
),
Flexible(
child: Container(
child: Text(
actionText,
overflow: TextOverflow.ellipsis,
),
),
)
],
),
),
Container(
child: Align(
child: Padding(
child: mediaPreview,
padding: EdgeInsets.all(15.0),
),
alignment: AlignmentDirectional.bottomEnd))
],
);
}
}
openImage(BuildContext context, String imageId) {
print("the image id is $imageId");
Navigator.of(context)
.push(MaterialPageRoute<bool>(builder: (BuildContext context) {
return Center(
child: Scaffold(
appBar: AppBar(
title: Text('Photo',
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold)),
backgroundColor: Colors.white,
),
body: ListView(
children: <Widget>[
Container(
child: ImagePostFromId(id: imageId),
),
],
)),
);
}));
}