2024-05-24 00:30:08 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-30 01:10:53 +02:00
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
2024-05-30 16:37:34 +02:00
|
|
|
|
2024-05-24 00:30:08 +02:00
|
|
|
import '../constants.dart';
|
2024-05-30 16:37:34 +02:00
|
|
|
import '../models/user_profile.dart';
|
|
|
|
import '../services/user_service.dart';
|
2024-05-30 01:10:53 +02:00
|
|
|
import 'edit_profile_page.dart';
|
2024-05-24 00:30:08 +02:00
|
|
|
|
|
|
|
class UserProfilePage extends StatefulWidget {
|
|
|
|
const UserProfilePage({super.key});
|
|
|
|
|
|
|
|
@override
|
2024-05-30 01:10:53 +02:00
|
|
|
State<UserProfilePage> createState() => _UserProfilePageState();
|
2024-05-24 00:30:08 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
class _UserProfilePageState extends State<UserProfilePage> {
|
|
|
|
String? profileImageUrl; // Track the profile image URL
|
2024-05-30 16:37:34 +02:00
|
|
|
late UserProfile myData;
|
2024-05-30 01:10:53 +02:00
|
|
|
bool isLoading = true;
|
2024-05-24 00:30:08 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-05-30 16:37:34 +02:00
|
|
|
// Load user data on initialization
|
|
|
|
_loadUserData();
|
2024-05-24 00:30:08 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
Future<void> _loadUserData() async {
|
2024-05-30 16:37:34 +02:00
|
|
|
myData = await UserService.getUserProfileById(
|
|
|
|
FirebaseAuth.instance.currentUser!.uid);
|
2024-05-26 01:44:49 +02:00
|
|
|
|
2024-05-26 17:33:28 +02:00
|
|
|
setState(() {
|
2024-05-30 01:10:53 +02:00
|
|
|
// Initialize the profile image URL
|
2024-05-30 16:37:34 +02:00
|
|
|
profileImageUrl = myData.profilePictureUrl;
|
2024-05-30 01:10:53 +02:00
|
|
|
// Set loading to false once data is loaded
|
|
|
|
isLoading = false;
|
2024-05-26 17:33:28 +02:00
|
|
|
});
|
2024-05-24 00:30:08 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
void editNameInfo() async {
|
|
|
|
final updatedUserData = await Navigator.push(
|
2024-05-25 13:56:45 +02:00
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
2024-05-30 16:37:34 +02:00
|
|
|
builder: (context) => EditProfilePage(userData: myData),
|
2024-05-25 13:56:45 +02:00
|
|
|
),
|
|
|
|
);
|
2024-05-30 01:10:53 +02:00
|
|
|
|
|
|
|
if (updatedUserData != null) {
|
|
|
|
setState(() {
|
2024-05-30 16:37:34 +02:00
|
|
|
// above Type of updatedUserData is dynamic, so check EditProfilePage
|
2024-05-30 01:10:53 +02:00
|
|
|
profileImageUrl = updatedUserData[Constants.dbFieldUsersProfilePic];
|
2024-05-30 16:37:34 +02:00
|
|
|
myData.profilePictureUrl =
|
|
|
|
updatedUserData[Constants.dbFieldUsersProfilePic];
|
|
|
|
myData.name = updatedUserData[Constants.dbFieldUsersName];
|
|
|
|
myData.bio = updatedUserData[Constants.dbFieldUsersBio];
|
2024-05-30 01:10:53 +02:00
|
|
|
});
|
|
|
|
}
|
2024-05-24 00:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-05-30 01:10:53 +02:00
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('User Profile'),
|
|
|
|
),
|
|
|
|
body: isLoading
|
|
|
|
? const Center(child: CircularProgressIndicator())
|
|
|
|
: Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.bottomRight,
|
|
|
|
child: OutlinedButton.icon(
|
|
|
|
label: const Text('Edit'),
|
|
|
|
icon: const Icon(Icons.edit),
|
|
|
|
onPressed: editNameInfo,
|
2024-05-25 13:56:45 +02:00
|
|
|
),
|
2024-05-30 01:10:53 +02:00
|
|
|
),
|
|
|
|
CircleAvatar(
|
|
|
|
radius: 50,
|
|
|
|
backgroundImage: profileImageUrl != null
|
|
|
|
? NetworkImage(profileImageUrl!)
|
|
|
|
: null,
|
|
|
|
child: profileImageUrl == null
|
|
|
|
? const Icon(Icons.person, size: 50)
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2024-05-30 16:37:34 +02:00
|
|
|
Text(myData.name, style: const TextStyle(fontSize: 24)),
|
|
|
|
Text(myData.email, style: const TextStyle(fontSize: 16)),
|
|
|
|
const SizedBox(height: 32),
|
2024-05-30 01:10:53 +02:00
|
|
|
Align(
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-05-24 00:30:08 +02:00
|
|
|
children: [
|
2024-05-30 16:37:34 +02:00
|
|
|
Text(
|
|
|
|
'Short description of yourself',
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.primary,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text(myData.bio ?? 'N/A',
|
2024-05-30 01:10:53 +02:00
|
|
|
style: const TextStyle(fontSize: 16)),
|
2024-05-24 00:30:08 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-05-30 01:10:53 +02:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
Divider(
|
|
|
|
color: Theme.of(context).colorScheme.primary,
|
2024-05-24 00:30:08 +02:00
|
|
|
),
|
2024-05-30 01:10:53 +02:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
],
|
|
|
|
),
|
2024-05-24 00:30:08 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|