189 lines
6.2 KiB
Dart
189 lines
6.2 KiB
Dart
import 'package:em2rp/providers/user_provider.dart';
|
|
import 'package:em2rp/utils/firebase_storage_manager.dart';
|
|
import 'package:em2rp/views/widgets/image/profile_picture.dart';
|
|
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
class MyAccountPage extends StatefulWidget {
|
|
const MyAccountPage({super.key});
|
|
@override
|
|
_MyAccountPageState createState() => _MyAccountPageState();
|
|
}
|
|
|
|
class _MyAccountPageState extends State<MyAccountPage> {
|
|
final User? user = FirebaseAuth.instance.currentUser;
|
|
final TextEditingController _firstNameController = TextEditingController();
|
|
final TextEditingController _lastNameController = TextEditingController();
|
|
final TextEditingController _phoneController = TextEditingController();
|
|
String? profilePhotoUrl;
|
|
bool _isHoveringProfilePic = false;
|
|
final FirebaseStorageManager _storageManager =
|
|
FirebaseStorageManager(); // Instance of FirebaseStorageManager
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadUserData();
|
|
}
|
|
|
|
Future<void> _loadUserData() async {
|
|
if (user != null) {
|
|
DocumentSnapshot userData = await FirebaseFirestore.instance
|
|
.collection('users')
|
|
.doc(user!.uid)
|
|
.get();
|
|
|
|
if (userData.exists) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_firstNameController.text = userData['firstName'] ?? '';
|
|
_lastNameController.text = userData['lastName'] ?? '';
|
|
_phoneController.text = userData['phone'] ?? '';
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _updateUserData() async {
|
|
if (user != null) {
|
|
await FirebaseFirestore.instance.collection('users').doc(user!.uid).set({
|
|
'firstName': _firstNameController.text,
|
|
'lastName': _lastNameController.text,
|
|
'phone': _phoneController.text,
|
|
}, SetOptions(merge: true));
|
|
}
|
|
}
|
|
|
|
Future<void> _changeProfilePicture() async {
|
|
final ImagePicker picker = ImagePicker();
|
|
final XFile? image = await picker.pickImage(
|
|
source: ImageSource.gallery,
|
|
); // You can also use ImageSource.camera
|
|
|
|
if (image != null) {
|
|
// Call FirebaseStorageManager to send the profile picture
|
|
String? newProfilePhotoUrl = await _storageManager.sendProfilePicture(
|
|
imageFile: image,
|
|
uid: user!.uid,
|
|
);
|
|
if (newProfilePhotoUrl != null) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
profilePhotoUrl =
|
|
newProfilePhotoUrl; // Update the profilePhotoUrl to refresh the UI
|
|
});
|
|
// Optionally, update the UserProvider if you are using it to manage user data globally
|
|
// Provider.of<UserProvider>(context, listen: false).setUserProfilePhotoUrl(newProfilePhotoUrl);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Photo de profil mise à jour avec succès!'),
|
|
),
|
|
);
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text(
|
|
'Erreur lors de la mise à jour de la photo de profil.',
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
// User cancelled image picking
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Sélection de photo annulée.')),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final userProvider = Provider.of<UserProvider>(
|
|
context,
|
|
); // Get UserProvider instance
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Gestion du compte')),
|
|
drawer: MainDrawer(
|
|
currentPage: '/my_account',
|
|
userProvider: userProvider,
|
|
), // Pass UserProvider to MainDrawer
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
MouseRegion(
|
|
onEnter: (_) => setState(() => _isHoveringProfilePic = true),
|
|
onExit: (_) => setState(() => _isHoveringProfilePic = false),
|
|
cursor: SystemMouseCursors.click,
|
|
child: GestureDetector(
|
|
onTap:
|
|
_changeProfilePicture, // Call _changeProfilePicture on tap
|
|
child: Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
ProfilePictureWidget(userId: user!.uid, radius: 45),
|
|
if (_isHoveringProfilePic)
|
|
Container(
|
|
width: 140,
|
|
height: 140,
|
|
decoration: BoxDecoration(
|
|
color: Colors.black54,
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: const Center(
|
|
child: Icon(
|
|
Icons.edit,
|
|
color: Colors.white,
|
|
size: 30,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
_buildTextField('Prénom', _firstNameController),
|
|
_buildTextField('Nom', _lastNameController),
|
|
_buildTextField('Numéro de téléphone', _phoneController),
|
|
_buildTextField(
|
|
'Email',
|
|
TextEditingController(text: user?.email ?? ''),
|
|
enabled: false,
|
|
),
|
|
const SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: _updateUserData,
|
|
child: const Text('Enregistrer'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTextField(
|
|
String label,
|
|
TextEditingController controller, {
|
|
bool enabled = true,
|
|
}) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
child: TextField(
|
|
controller: controller,
|
|
enabled: enabled,
|
|
decoration: InputDecoration(
|
|
labelText: label,
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|