cpd_job_app/lib/Search/profile_company.dart

286 lines
8.9 KiB
Dart
Raw Permalink Normal View History

2023-06-18 16:56:38 +02:00
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
2023-06-19 10:35:42 +02:00
import 'package:url_launcher/url_launcher_string.dart';
import '../Widgets/bottom_nav_bar.dart';
import '../services/global_variables.dart';
class ProfileScreen extends StatefulWidget {
2023-06-20 03:23:54 +02:00
final String userID;
2023-06-18 16:56:38 +02:00
2023-06-20 03:23:54 +02:00
const ProfileScreen({required this.userID});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
2023-06-18 16:56:38 +02:00
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
TextEditingController _nameController = TextEditingController();
TextEditingController _emailController = TextEditingController();
TextEditingController _phoneNumberController = TextEditingController();
TextEditingController _locationController = TextEditingController();
String? name;
String? email;
String? phoneNumber;
String? imageUrl;
String? joinedAt;
String? location;
bool _isLoading = false;
bool _isSameUser = false;
bool _isEditing = false;
@override
void initState() {
super.initState();
getUserData();
}
2023-06-19 10:35:42 +02:00
void _sendEmail() async {
{
final Uri params = Uri(
scheme: "mailto",
path: email,
query: "subject=Hey from JobFinder",
2023-06-19 10:35:42 +02:00
);
final url = params.toString();
launchUrlString(url);
}
}
2023-06-18 16:56:38 +02:00
void getUserData() async {
try {
setState(() {
_isLoading = true;
});
final DocumentSnapshot userDoc = await FirebaseFirestore.instance
.collection('users')
2023-06-20 03:23:54 +02:00
.doc(widget.userID)
.get();
2023-06-18 16:56:38 +02:00
if (userDoc.exists) {
2023-06-18 16:56:38 +02:00
setState(() {
name = userDoc.get('name');
email = userDoc.get('email');
phoneNumber = userDoc.get('phone');
imageUrl = userDoc.get('userImage');
location = userDoc.get('location');
2023-06-18 16:56:38 +02:00
_nameController.text = name ?? '';
_emailController.text = email ?? '';
_phoneNumberController.text = phoneNumber ?? '';
_locationController.text = location ?? '';
_isLoading = false;
final timestamp = userDoc.get('createdAt');
2023-06-18 16:56:38 +02:00
});
User? user = _auth.currentUser;
final _uid = user!.uid;
setState(() {
2023-06-20 03:23:54 +02:00
_isSameUser = _uid == widget.userID;
2023-06-18 16:56:38 +02:00
});
} else {
print('User not found');
2023-06-18 16:56:38 +02:00
}
} catch (error) {
// Handle error
print('Error: $error');
2023-06-18 16:56:38 +02:00
} finally {
setState(() {
_isLoading = false;
});
}
}
void _toggleEditMode() {
if (_isSameUser) {
setState(() {
_isEditing = !_isEditing;
});
}
setState(() {});
2023-06-18 16:56:38 +02:00
}
void _saveChanges() {
setState(() {
name = _nameController.text;
email = _emailController.text;
phoneNumber = _phoneNumberController.text;
location = _locationController.text;
_toggleEditMode();
});
// Update data in Firebase
if (_isSameUser) {
2023-06-20 03:23:54 +02:00
_firestore.collection('users').doc(widget.userID).update({
2023-06-18 16:56:38 +02:00
'name': name,
'email': email,
'phone': phoneNumber,
'location': location,
}).then((value) {
// Success
}).catchError((error) {
// Handle error
});
}
}
@override
Widget build(BuildContext context) {
2023-06-18 16:56:38 +02:00
return Scaffold(
appBar: AppBar(
2023-06-20 03:23:54 +02:00
title: const Text('Profile'),
2023-06-18 16:56:38 +02:00
actions: [
if (_isSameUser)
ElevatedButton(
onPressed: _saveChanges,
child: Text(_isEditing ? 'Save' : 'Edit'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
Colors.blue,
),
foregroundColor: MaterialStateProperty.all<Color>(
Colors.white,
),
padding: MaterialStateProperty.all<EdgeInsets>(
2023-06-20 03:23:54 +02:00
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
),
2023-06-18 16:56:38 +02:00
),
2023-06-19 10:35:42 +02:00
if (!_isSameUser && email != null)
IconButton(
onPressed: _sendEmail,
2023-06-20 03:23:54 +02:00
icon: const Icon(Icons.mail),
2023-06-19 10:35:42 +02:00
),
2023-06-18 16:56:38 +02:00
],
),
bottomNavigationBar: BottomNavigationBarForApp(indexNum: 3),
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Colors.cyan, Colors.white60],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
stops: [0.2, 0.9],
2023-06-18 16:56:38 +02:00
),
),
child: Stack(
children: [
Image.network(
signupUrlImage,
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
2023-06-18 16:56:38 +02:00
SingleChildScrollView(
child: Column(
children: [
2023-06-20 03:23:54 +02:00
const SizedBox(height: 40),
2023-06-18 16:56:38 +02:00
Container(
2023-06-20 03:23:54 +02:00
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width,
decoration: const BoxDecoration(
2023-06-18 16:56:38 +02:00
shape: BoxShape.circle,
),
child: ClipOval(
child: imageUrl != null
? Image.network(
2023-06-20 03:23:54 +02:00
imageUrl!,
fit: BoxFit.cover,
)
2023-06-18 16:56:38 +02:00
: Image.asset(
2023-06-20 03:23:54 +02:00
'assets/images/signup.png',
fit: BoxFit.cover,
),
2023-06-18 16:56:38 +02:00
),
),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 24),
2023-06-18 16:56:38 +02:00
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
2023-06-18 16:56:38 +02:00
children: [
2023-06-20 03:23:54 +02:00
const Text(
2023-06-18 16:56:38 +02:00
'Name:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 4),
2023-06-18 16:56:38 +02:00
_isEditing && _isSameUser
? TextFormField(
2023-06-20 03:23:54 +02:00
controller: _nameController,
)
2023-06-18 16:56:38 +02:00
: Text(name ?? ''),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 16),
const Text(
2023-06-18 16:56:38 +02:00
'Email:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 4),
2023-06-18 16:56:38 +02:00
_isEditing && _isSameUser
? TextFormField(
2023-06-20 03:23:54 +02:00
controller: _emailController,
)
2023-06-18 16:56:38 +02:00
: Text(email ?? ''),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 16),
const Text(
2023-06-18 16:56:38 +02:00
'Phone Number:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 4),
2023-06-18 16:56:38 +02:00
_isEditing && _isSameUser
? TextFormField(
2023-06-20 03:23:54 +02:00
controller: _phoneNumberController,
)
2023-06-18 16:56:38 +02:00
: Text(phoneNumber ?? ''),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 16),
const Text(
2023-06-18 16:56:38 +02:00
'Location:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
2023-06-20 03:23:54 +02:00
const SizedBox(height: 4),
2023-06-18 16:56:38 +02:00
_isEditing && _isSameUser
? TextFormField(
2023-06-20 03:23:54 +02:00
controller: _locationController,
)
2023-06-18 16:56:38 +02:00
: Text(location ?? ''),
],
),
),
],
),
),
2023-06-18 16:56:38 +02:00
if (_isLoading)
Container(
color: Colors.black.withOpacity(0.5),
2023-06-20 03:23:54 +02:00
child: const Center(
2023-06-18 16:56:38 +02:00
child: CircularProgressIndicator(),
),
),
],
),
),
);
}
}