File tree Expand file tree Collapse file tree 4 files changed +81
-18
lines changed Expand file tree Collapse file tree 4 files changed +81
-18
lines changed Original file line number Diff line number Diff line change
1
+ import 'package:cloud_firestore/cloud_firestore.dart' ;
2
+ import 'package:firebase_auth/firebase_auth.dart' ;
3
+
4
+ class FireStoreMethods {
5
+ final FirebaseFirestore _firestore = FirebaseFirestore .instance;
6
+ Future <void > followUser (String uid, String followId) async {
7
+ try {
8
+ DocumentSnapshot snapshot =
9
+ await _firestore.collection ('Users' ).doc (uid).get ();
10
+ var data = snapshot.data () as Map <String , dynamic >;
11
+ List following = data['following' ];
12
+
13
+ if (following.contains (followId)) {
14
+ await _firestore.collection ('Users' ).doc (followId).update ({
15
+ 'followers' : FieldValue .arrayRemove ([uid]),
16
+ });
17
+ await _firestore.collection ('Users' ).doc (uid).update ({
18
+ 'following' : FieldValue .arrayRemove ([followId]),
19
+ });
20
+ } else {
21
+ await _firestore.collection ('Users' ).doc (followId).update ({
22
+ 'followers' : FieldValue .arrayUnion ([uid]),
23
+ });
24
+ await _firestore.collection ('Users' ).doc (uid).update ({
25
+ 'following' : FieldValue .arrayUnion ([followId]),
26
+ });
27
+ }
28
+ } catch (e) {
29
+ print (e.toString ());
30
+ }
31
+ }
32
+
33
+ Future <void > signOut () async {
34
+ await FirebaseAuth .instance.signOut ();
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -15,31 +15,42 @@ class ResponsiveLayout extends StatefulWidget {
15
15
}
16
16
17
17
class _ResponsiveLayoutState extends State <ResponsiveLayout > {
18
+ bool isloading = false ;
18
19
@override
19
20
void initState () {
20
21
super .initState ();
21
22
userData ();
22
23
}
23
24
24
25
userData () async {
26
+ setState (() {
27
+ isloading = true ;
28
+ });
25
29
UserProvider userProvider = Provider .of (context, listen: false );
26
30
await userProvider.refreshUserDetails ();
31
+ setState (() {
32
+ isloading = false ;
33
+ });
27
34
// print(3);
28
35
// print(userProvider.getUser.profileUrl);
29
36
}
30
37
31
38
@override
32
39
Widget build (BuildContext context) {
33
- return LayoutBuilder (
34
- builder: (context, constraints) {
35
- if (constraints.maxWidth > 600 ) {
36
- // display web screen layout
37
- return widget.webScreenLayout;
38
- } else {
39
- // display mobile screen layout
40
- return widget.mobileScreenLayout;
41
- }
42
- },
43
- );
40
+ return isloading
41
+ ? const Center (
42
+ child: CircularProgressIndicator (),
43
+ )
44
+ : LayoutBuilder (
45
+ builder: (context, constraints) {
46
+ if (constraints.maxWidth > 600 ) {
47
+ // display web screen layout
48
+ return widget.webScreenLayout;
49
+ } else {
50
+ // display mobile screen layout
51
+ return widget.mobileScreenLayout;
52
+ }
53
+ },
54
+ );
44
55
}
45
56
}
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import 'package:firebase_auth/firebase_auth.dart';
5
5
import 'package:flutter/material.dart' ;
6
6
import 'package:image_picker/image_picker.dart' ;
7
7
import 'package:instagram_clone/backend/providers/user_providers.dart' ;
8
+ import 'package:instagram_clone/backend/storage/firestore_methods.dart' ;
8
9
import 'package:instagram_clone/model/user.dart' ;
9
10
// import 'package:flutter/widgets.dart';
10
11
import 'package:instagram_clone/utils/colors.dart' ;
@@ -152,23 +153,38 @@ class _ProfileScreenState extends State<ProfileScreen> {
152
153
user! .uid == widget.uid
153
154
? CustomButton (
154
155
function: () {},
155
- text: 'Share ' ,
156
+ text: 'Edit ' ,
156
157
backgroundcolor: mobileBackgroundColor,
157
158
textColor: primaryColor)
158
159
: isFollowing
159
160
? CustomButton (
160
- function: () {},
161
+ function: () async {
162
+ await FireStoreMethods ()
163
+ .followUser (user! .uid, widget.uid);
164
+
165
+ setState (() {
166
+ isFollowing = false ;
167
+ noOfFollowers-- ;
168
+ });
169
+ },
161
170
text: 'following' ,
162
171
backgroundcolor: Colors .white,
163
172
textColor: Colors .black)
164
173
: CustomButton (
165
- function: () {},
174
+ function: () async {
175
+ await FireStoreMethods ()
176
+ .followUser (user! .uid, widget.uid);
177
+ setState (() {
178
+ isFollowing = true ;
179
+ noOfFollowers++ ;
180
+ });
181
+ },
166
182
text: 'follow' ,
167
183
backgroundcolor: Colors .blue,
168
184
textColor: primaryColor),
169
185
CustomButton (
170
186
function: () {},
171
- text: 'Edit ' ,
187
+ text: 'share ' ,
172
188
backgroundcolor: mobileBackgroundColor,
173
189
textColor: primaryColor),
174
190
],
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class CustomButton extends StatelessWidget {
5
5
final String text;
6
6
final Color backgroundcolor;
7
7
final Color textColor;
8
- final Function function;
8
+ final Function () function;
9
9
const CustomButton (
10
10
{super .key,
11
11
required this .function,
@@ -16,7 +16,7 @@ class CustomButton extends StatelessWidget {
16
16
@override
17
17
Widget build (BuildContext context) {
18
18
return InkWell (
19
- onTap: () => function,
19
+ onTap: function,
20
20
borderRadius: BorderRadius .circular (15 ),
21
21
splashColor: Colors .white,
22
22
child: Container (
@@ -34,7 +34,7 @@ class CustomButton extends StatelessWidget {
34
34
style: TextStyle (
35
35
backgroundColor: backgroundcolor,
36
36
color: textColor,
37
- fontWeight: FontWeight .w800 ),
37
+ fontWeight: FontWeight .bold ),
38
38
),
39
39
),
40
40
),
You can’t perform that action at this time.
0 commit comments