2024-09-06 14:13:30 +01:00
|
|
|
|
|
|
|
import 'dart:io';
|
2024-08-26 14:49:06 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-09-06 14:13:30 +01:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
2024-08-26 14:49:06 +01:00
|
|
|
import 'package:mobile/models.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'api.dart' as api;
|
|
|
|
import 'base/variables.dart';
|
|
|
|
|
|
|
|
class EditProfilePage extends StatefulWidget {
|
|
|
|
final User? userData;
|
2024-09-06 14:13:30 +01:00
|
|
|
|
2024-08-26 14:49:06 +01:00
|
|
|
const EditProfilePage({super.key, required this.userData});
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<EditProfilePage> createState() => _ProfilePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ProfilePageState extends State<EditProfilePage> {
|
|
|
|
TextEditingController usernameInput = TextEditingController();
|
|
|
|
TextEditingController emailInput = TextEditingController();
|
|
|
|
TextEditingController passwordInput = TextEditingController();
|
|
|
|
TextEditingController confirmPasswordInput = TextEditingController();
|
2024-09-06 14:13:30 +01:00
|
|
|
File? _selectedImage;
|
|
|
|
|
2024-08-27 12:06:31 +01:00
|
|
|
set userData(User userData) {}
|
2024-08-26 14:49:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
// Initialize the controllers with existing data
|
|
|
|
usernameInput.text = widget.userData!.username;
|
|
|
|
emailInput.text = widget.userData!.email;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
// Dispose of the controllers when the widget is disposed
|
|
|
|
usernameInput.dispose();
|
|
|
|
emailInput.dispose();
|
|
|
|
passwordInput.dispose();
|
|
|
|
confirmPasswordInput.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:36:32 +01:00
|
|
|
Future _pickImageFromGallery() async{
|
|
|
|
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
|
|
|
|
if (image == null) return;
|
2024-09-06 14:13:30 +01:00
|
|
|
setState(() {
|
2024-09-09 14:36:32 +01:00
|
|
|
_selectedImage = File(image.path);
|
|
|
|
});
|
2024-09-06 14:13:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future _pickImageFromCamera() async{
|
2024-09-09 14:36:32 +01:00
|
|
|
final image = await ImagePicker().pickImage(source: ImageSource.camera);
|
|
|
|
if (image == null) return;
|
2024-09-06 14:13:30 +01:00
|
|
|
setState(() {
|
2024-09-09 14:36:32 +01:00
|
|
|
_selectedImage = File(image.path);
|
|
|
|
});
|
2024-09-06 14:13:30 +01:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:36:32 +01:00
|
|
|
|
2024-09-06 14:13:30 +01:00
|
|
|
|
2024-09-09 14:36:32 +01:00
|
|
|
void _saveProfile() async {
|
|
|
|
if (passwordInput.text != confirmPasswordInput.text) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(content: Text('Passwords do not match')));
|
|
|
|
return;
|
2024-09-06 14:13:30 +01:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:36:32 +01:00
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
String? id = prefs.getString('id');
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
if (id != null) {
|
|
|
|
final response = await api.putUser(
|
|
|
|
context,
|
|
|
|
api.ApiService.auth,
|
|
|
|
'PUT',
|
|
|
|
'/api/users',
|
|
|
|
{
|
|
|
|
'id': id,
|
|
|
|
'username': usernameInput.text,
|
|
|
|
'email': emailInput.text,
|
|
|
|
'password': passwordInput.text,
|
|
|
|
},
|
|
|
|
_selectedImage // Pass the selected image to the putUser function
|
|
|
|
);
|
2024-08-26 14:49:06 +01:00
|
|
|
|
2024-09-09 14:36:32 +01:00
|
|
|
if (!mounted) return;
|
2024-08-26 14:49:06 +01:00
|
|
|
|
2024-08-27 07:40:54 +01:00
|
|
|
if (response != null) {
|
2024-08-27 12:06:31 +01:00
|
|
|
setState(() {
|
2024-09-09 14:36:32 +01:00
|
|
|
user = null;
|
2024-08-27 12:06:31 +01:00
|
|
|
});
|
2024-08-26 14:49:06 +01:00
|
|
|
|
2024-08-27 07:40:54 +01:00
|
|
|
Navigator.of(context).pop(); // Close the dialog
|
2024-08-27 12:06:31 +01:00
|
|
|
Navigator.pushReplacementNamed(context, '/profile');
|
2024-08-27 07:40:54 +01:00
|
|
|
} else {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(content: Text('Something went wrong! Please contact an admin.')),
|
|
|
|
);
|
2024-09-06 14:13:30 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-09 14:36:32 +01:00
|
|
|
}
|
|
|
|
|
2024-08-26 14:49:06 +01:00
|
|
|
|
|
|
|
void _deleteProfile(BuildContext context) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text('Confirm Deletion'),
|
|
|
|
content: Text('Are you sure you want to delete your profile?'),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
child: Text('Cancel'),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop(); // Close the dialog
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
child: Text('Delete'),
|
|
|
|
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
|
|
|
onPressed: () async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
String? id = prefs.getString('id');
|
|
|
|
|
2024-08-27 07:40:54 +01:00
|
|
|
final response = await api.request(context, api.ApiService.auth, 'DELETE', '/api/users/$id', null);
|
2024-08-26 14:49:06 +01:00
|
|
|
|
2024-08-27 07:40:54 +01:00
|
|
|
if (response != null) {
|
2024-08-26 14:49:06 +01:00
|
|
|
prefs.remove('token');
|
2024-08-27 07:40:54 +01:00
|
|
|
prefs.remove('id');
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
user = null;
|
2024-08-26 14:49:06 +01:00
|
|
|
});
|
2024-08-27 07:40:54 +01:00
|
|
|
|
|
|
|
Navigator.of(context).pop(); // Close the dialog
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
|
|
Navigator.pushReplacementNamed(context, '/register');
|
2024-08-26 14:49:06 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-08-27 07:40:54 +01:00
|
|
|
return Scaffold(
|
2024-08-26 14:49:06 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: Text('Edit Profile'),
|
|
|
|
leading: IconButton(
|
|
|
|
icon: Icon(Icons.arrow_back),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context); // Navigates back when the back button is pressed
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
body: Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
TextField(
|
|
|
|
controller: usernameInput,
|
|
|
|
decoration: InputDecoration(labelText: 'Name'),
|
|
|
|
),
|
|
|
|
TextField(
|
|
|
|
controller: emailInput,
|
|
|
|
decoration: InputDecoration(labelText: 'Email'),
|
|
|
|
),
|
|
|
|
TextField(
|
|
|
|
controller: passwordInput,
|
|
|
|
decoration: InputDecoration(labelText: 'New password'),
|
|
|
|
),
|
|
|
|
TextField(
|
|
|
|
controller: confirmPasswordInput,
|
|
|
|
decoration: InputDecoration(labelText: 'Repeat new password'),
|
|
|
|
),
|
2024-09-09 15:48:37 +01:00
|
|
|
SizedBox(height: 20),
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-09-06 14:13:30 +01:00
|
|
|
children: [
|
2024-09-09 15:48:37 +01:00
|
|
|
Text('ProfilePicture',
|
|
|
|
style: TextStyle(fontSize: 17)),
|
|
|
|
if (_selectedImage != null)
|
|
|
|
ClipOval(
|
|
|
|
child: Image(
|
|
|
|
image: FileImage(_selectedImage!),
|
|
|
|
height: 100,
|
|
|
|
width: 100,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else
|
|
|
|
ClipOval(
|
|
|
|
child: Image(
|
|
|
|
image: NetworkImage(user!.profilePicture),
|
|
|
|
height: 100,
|
|
|
|
width: 100,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (_selectedImage != null)
|
|
|
|
Text(_selectedImage!.path.toString()),
|
|
|
|
//until here
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Text('Change using'),
|
|
|
|
TextButton(onPressed: _pickImageFromGallery, child: Text('Gallery')),
|
|
|
|
SizedBox(width: 5),
|
|
|
|
Text('or'),
|
|
|
|
SizedBox(width: 5),
|
|
|
|
TextButton(onPressed: _pickImageFromCamera, child: Text('Camera'))
|
|
|
|
],
|
|
|
|
),
|
2024-09-06 14:13:30 +01:00
|
|
|
],
|
|
|
|
),
|
2024-09-09 15:48:37 +01:00
|
|
|
SizedBox(height: 40),
|
2024-08-26 14:49:06 +01:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: _saveProfile, // Save and pop
|
|
|
|
child: Text('Save'),
|
|
|
|
),
|
|
|
|
SizedBox(width: 10),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () => _deleteProfile(context),
|
|
|
|
child: Text('Delete'),
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
foregroundColor: Colors.red, // Red text
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|