cofounderella/lib/pages/user_profile_page.dart

119 lines
4.0 KiB
Dart
Raw Normal View History

2024-05-24 00:30:08 +02:00
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
2024-05-24 00:30:08 +02:00
import '../constants.dart';
import 'edit_profile_page.dart';
2024-05-24 00:30:08 +02:00
class UserProfilePage extends StatefulWidget {
const UserProfilePage({super.key});
@override
State<UserProfilePage> createState() => _UserProfilePageState();
2024-05-24 00:30:08 +02:00
}
class _UserProfilePageState extends State<UserProfilePage> {
String? profileImageUrl; // Track the profile image URL
bool isLoading = true;
late Map<String, dynamic> userData;
2024-05-24 00:30:08 +02:00
@override
void initState() {
super.initState();
_loadUserData(); // Load user data on initialization
2024-05-24 00:30:08 +02:00
}
Future<void> _loadUserData() async {
DocumentSnapshot userDoc = await FirebaseFirestore.instance
2024-05-24 00:30:08 +02:00
.collection(Constants.dbCollectionUsers)
.doc(FirebaseAuth.instance.currentUser!.uid)
2024-05-24 00:30:08 +02:00
.get();
2024-05-26 01:44:49 +02:00
setState(() {
userData = userDoc.data() as Map<String, dynamic>;
// Initialize the profile image URL
profileImageUrl = userData[Constants.dbFieldUsersProfilePic];
// Set loading to false once data is loaded
isLoading = false;
});
2024-05-24 00:30:08 +02:00
}
void editNameInfo() async {
final updatedUserData = await Navigator.push(
2024-05-25 13:56:45 +02:00
context,
MaterialPageRoute(
builder: (context) => EditProfilePage(userData: userData),
2024-05-25 13:56:45 +02:00
),
);
if (updatedUserData != null) {
setState(() {
profileImageUrl = updatedUserData[Constants.dbFieldUsersProfilePic];
userData[Constants.dbFieldUsersName] =
updatedUserData[Constants.dbFieldUsersName];
userData[Constants.dbFieldUsersBio] =
updatedUserData[Constants.dbFieldUsersBio];
});
}
2024-05-24 00:30:08 +02:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('User Profile'),
),
body: isLoading
? const Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
//crossAxisAlignment: CrossAxisAlignment.start,
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
),
),
CircleAvatar(
radius: 50,
backgroundImage: profileImageUrl != null
? NetworkImage(profileImageUrl!)
: null,
child: profileImageUrl == null
? const Icon(Icons.person, size: 50)
: null,
),
const SizedBox(height: 16),
Text(userData[Constants.dbFieldUsersName] ?? 'Name',
style: const TextStyle(fontSize: 24)),
Text(userData[Constants.dbFieldUsersEmail] ?? 'Email',
style: const TextStyle(fontSize: 16)),
const SizedBox(height: 16),
Align(
alignment: Alignment.centerLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2024-05-24 00:30:08 +02:00
children: [
const Text('Bio'),
Text(userData[Constants.dbFieldUsersBio] ?? 'N/A',
style: const TextStyle(fontSize: 16)),
2024-05-24 00:30:08 +02:00
],
),
),
const SizedBox(height: 16),
Divider(
color: Theme.of(context).colorScheme.primary,
2024-05-24 00:30:08 +02:00
),
const SizedBox(height: 16),
],
),
2024-05-24 00:30:08 +02:00
),
),
);
}
}