Future signInFunction() async {
GoogleSignInAccount? googleUser = await googleSignIn.signIn();
if (googleUser == null) {
return;
}
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);
DocumentSnapshot userExist =
await firestore.collection('users').doc(userCredential.user!.uid).get();
if (userExist.exists) {
print("User Already Exists in Database");
} else {
try {
await firestore.collection('users').doc(userCredential.user!.uid).set({
'email': userCredential.user!.email,
"username": userCredential.user!.email,
'fullName': userCredential.user!.displayName,
'image': userCredential.user!.photoURL,
'uid': userCredential.user!.uid,
'date': DateTime.now(),
"followers": [],
"followings": [],
"bio": "Enter bio"
});
} on FirebaseException catch (e) {
print(e.toString());
}
}
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.orderBy('datePublished', descending: true)
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.hasError) {
return Center(
child: Text("Error"),
);
} else if (!snapshot.hasData) {
return Center(
child: Text("No Uploaded Image"),
);
} else {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) =>
PostCard(snap: snapshot.data!.docs[index].data()),
);
}
} else {
return Text('State: ${snapshot.connectionState}');
}
});
SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(16).copyWith(top: 0),
child: Row(
children: [
Container(
child: CircleAvatar(
backgroundImage: NetworkImage(profileModel.image),
radius: 40,
),
),
Expanded(
child: Column(
children: [
SizedBox(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(padding: EdgeInsets.only(left: 25)),
buildStatColumn('Posts', postLen),
buildStatColumn('followers', followers),
buildStatColumn('following', following),
],
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child: FirebaseAuth
.instance.currentUser!.uid ==
widget.uid
? FollowButton(
backgroundColor: Colors.transparent,
borderColor: Colors.black,
text: 'Edit Profile',
function: () {
Get.to(() => EditProfileScreen(
snap: profileModel,
));
},
textColor: Colors.black)
: isFollowing
? FollowButton(
backgroundColor:
Colors.transparent,
borderColor: Colors.black,
text: 'unfollow',
textColor: Colors.black)
: FollowButton(
backgroundColor: Colors.blue,
borderColor: Colors.black,
text: 'follow',
textColor: Colors.black),
),
],
),
],
),
),
],
),
),
Container(
padding: EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.bottomLeft,
child: Text(
profileModel.fullName,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Container(
padding:
EdgeInsets.symmetric(horizontal: 20).copyWith(top: 10),
alignment: Alignment.bottomLeft,
child: Text(
profileModel.bio,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
new Divider(
color: Colors.grey.shade400,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
padding: EdgeInsets.symmetric(horizontal: 30),
onPressed: () {},
icon: Icon(
Icons.video_label_outlined,
size: 30,
color: Colors.grey.shade600,
)),
IconButton(
padding: EdgeInsets.symmetric(horizontal: 30),
onPressed: () {},
icon: Icon(Icons.video_library_outlined,
size: 30, color: Colors.grey.shade600)),
],
),
Expanded(
child: FutureBuilder(
future: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.get(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator());
} else if (snapshot.connectionState ==
ConnectionState.done) {
if (snapshot.hasError) {
return const Text('Error');
} else if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.only(
left: 4.0, right: 4.0),
child: GridView.builder(
shrinkWrap: true,
itemCount:
(snapshot.data! as dynamic).docs.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 3,
mainAxisSpacing: 3),
itemBuilder: (context, index) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(),
borderRadius:
BorderRadius.circular(0),
),
child: Image(
loadingBuilder:
(BuildContext context,
Widget child,
ImageChunkEvent?
loadingProgress) {
if (loadingProgress == null) {
return child;
}
return Center(
child:
CircularProgressIndicator(
value: loadingProgress
.expectedTotalBytes !=
null
? loadingProgress
.cumulativeBytesLoaded /
loadingProgress
.expectedTotalBytes!
: null,
),
);
},
image: NetworkImage(
(snapshot.data! as dynamic)
.docs[index]['postURL']),
fit: BoxFit.cover,
));
}),
);
} else {
return const Text('Empty data');
}
} else {
return SizedBox(
height: Constant.height / 2,
width: Constant.width / 2,
child: const Text("else"));
}
}),
)
],
),
),