From 360281c2833be617497530e1cd41e3d3b9679f96 Mon Sep 17 00:00:00 2001 From: Ashwin R Nair <85173652+AshwinRNair-ARN@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:45:26 +0530 Subject: [PATCH] Profile Photo Backend Fix --- lib/app/views/screens/auth/user_detail.dart | 6 +- lib/app/views/screens/profile/profile.dart | 62 ++++++++++++++++++--- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/app/views/screens/auth/user_detail.dart b/lib/app/views/screens/auth/user_detail.dart index f987753..0720dde 100644 --- a/lib/app/views/screens/auth/user_detail.dart +++ b/lib/app/views/screens/auth/user_detail.dart @@ -21,7 +21,6 @@ class UserDetailsScreen extends StatefulWidget { class _UserDetailsScreenState extends State { final formKey = GlobalKey(); - late TextEditingController yearOfJoiningController; late TextEditingController yearOfGraduationController; late TextEditingController collegeController; late TextEditingController companyController; @@ -45,15 +44,13 @@ class _UserDetailsScreenState extends State { @override void initState() { super.initState(); - yearOfJoiningController = TextEditingController(); yearOfGraduationController = TextEditingController(); collegeController = TextEditingController(); companyController = TextEditingController(); } @override - void dispose() { - yearOfJoiningController.dispose(); + void dispose() {; yearOfGraduationController.dispose(); collegeController.dispose(); companyController.dispose(); @@ -150,7 +147,6 @@ class _UserDetailsScreenState extends State { 'name': widget.name, // Passed from the previous screen 'email': widget.email, // Passed from the previous screen - 'yearOfJoining': yearOfJoiningController.text, 'yearOfGraduation': yearOfGraduationController.text, 'course': selectedCourse, 'branch': selectedBranch, diff --git a/lib/app/views/screens/profile/profile.dart b/lib/app/views/screens/profile/profile.dart index 006b127..703cdf7 100644 --- a/lib/app/views/screens/profile/profile.dart +++ b/lib/app/views/screens/profile/profile.dart @@ -5,6 +5,8 @@ import 'package:image_picker/image_picker.dart'; import 'dart:io'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_storage/firebase_storage.dart' as firebase_storage; +import 'package:path/path.dart' as Path; class ProfileScreen extends StatefulWidget { const ProfileScreen({super.key}); @@ -110,8 +112,7 @@ class _ProfileScreenState extends State { .get(); if (userProfile.exists) { - Map userData = - userProfile.data() as Map; + Map userData = userProfile.data() as Map; _nameController.text = userData['name'] ?? ''; _emailController.text = userData['email'] ?? ''; @@ -119,9 +120,15 @@ class _ProfileScreenState extends State { _companyController.text = userData['company'] ?? 'Not available'; _branchController.text = userData['branch'] ?? 'Not available'; _courseController.text = userData['course'] ?? 'Not available'; - _yearOfGraduationController.text = - userData['yearOfGraduation'] ?? 'Not available'; - + _yearOfGraduationController.text = userData['yearOfGraduation'] ?? 'Not available'; + + // Set the profile image URL if available + if (userData.containsKey('photoUrl') && userData['photoUrl'] != null && userData['photoUrl'].toString().isNotEmpty) { + setState(() { + _profileImage = XFile(userData['photoUrl']); + }); + } + bool isMentor = userData['isMentor'] ?? false; setState(() { @@ -134,6 +141,37 @@ class _ProfileScreenState extends State { } } + Future _uploadProfileImage() async { + User? user = FirebaseAuth.instance.currentUser; + if (user != null && _profileImage != null) { + try { + String fileName = Path.basename(_profileImage!.path); + firebase_storage.Reference firebaseStorageRef = + firebase_storage.FirebaseStorage.instance.ref().child('profile_images/${user.uid}/$fileName'); + + firebase_storage.UploadTask uploadTask = firebaseStorageRef.putFile(File(_profileImage!.path)); + await uploadTask; + + // Once the image is uploaded, get the download URL + String downloadURL = await firebaseStorageRef.getDownloadURL(); + + // Update the Firestore user document with the new image URL + await FirebaseFirestore.instance.collection('users').doc(user.uid).update({ + 'photoUrl': downloadURL, + }); + + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Profile picture updated successfully')), + ); + } catch (e) { + print('Error uploading image: $e'); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Error updating profile picture')), + ); + } + } + } + Future _pickImage() async { try { final pickedFile = await _picker.pickImage(source: ImageSource.gallery); @@ -141,9 +179,15 @@ class _ProfileScreenState extends State { setState(() { _profileImage = pickedFile; }); + + // After setting the profile image, upload it + await _uploadProfileImage(); } } catch (e) { print('Error picking image: $e'); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Error picking image')), + ); } } @@ -195,10 +239,10 @@ class _ProfileScreenState extends State { CircleAvatar( radius: 60, // Radius of the profile picture backgroundImage: _profileImage != null - ? FileImage(File(_profileImage!.path)) - as ImageProvider - : AssetImage('assets/profile_image.png') - as ImageProvider, + ? (_profileImage!.path.startsWith('http') // Check if it's a URL + ? NetworkImage(_profileImage!.path) as ImageProvider + : FileImage(File(_profileImage!.path)) as ImageProvider) + : AssetImage('assets/profile_image.png') as ImageProvider, // Default profile image ), Positioned( right: