2024-08-23 14:19:15 +01:00
|
|
|
import 'dart:convert';
|
2024-08-21 12:28:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-08-23 11:34:38 +01:00
|
|
|
import 'package:mobile/base/variables.dart';
|
2024-09-10 14:44:50 +01:00
|
|
|
import 'package:mobile/models.dart' as models;
|
2024-08-23 14:19:15 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2024-08-22 12:28:24 +01:00
|
|
|
import 'base/sidemenu.dart';
|
2024-08-23 14:19:15 +01:00
|
|
|
import 'api.dart' as api;
|
2024-08-26 14:49:06 +01:00
|
|
|
import 'editprofile.dart';
|
2024-08-22 12:28:24 +01:00
|
|
|
|
|
|
|
class ProfilePage extends StatefulWidget {
|
2024-08-23 11:34:38 +01:00
|
|
|
const ProfilePage({super.key});
|
2024-08-22 12:28:24 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<ProfilePage> createState() => _ProfilePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ProfilePageState extends State<ProfilePage> {
|
2024-09-10 14:44:50 +01:00
|
|
|
models.User? userData;
|
2024-08-22 12:28:24 +01:00
|
|
|
|
2024-08-23 11:34:38 +01:00
|
|
|
@override
|
2024-08-23 14:19:15 +01:00
|
|
|
void initState() {
|
2024-08-22 12:28:24 +01:00
|
|
|
super.initState();
|
2024-08-23 14:19:15 +01:00
|
|
|
getProfile();
|
|
|
|
}
|
2024-08-22 12:28:24 +01:00
|
|
|
|
2024-08-23 14:19:15 +01:00
|
|
|
Future<void> getProfile() async {
|
|
|
|
if (user != null) {
|
|
|
|
setState(() {
|
|
|
|
userData = user!;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
String? id = prefs.getString('id');
|
|
|
|
|
|
|
|
if (id != null) {
|
|
|
|
final response = await api
|
|
|
|
.request(context, api.ApiService.auth, 'GET', '/api/users/$id', null);
|
|
|
|
|
|
|
|
if (response == null) return;
|
2024-08-22 12:28:24 +01:00
|
|
|
|
2024-08-23 14:19:15 +01:00
|
|
|
Map<String, dynamic> json = jsonDecode(response);
|
2024-09-10 14:44:50 +01:00
|
|
|
models.User jsonUser = models.User.fromJson(json);
|
2024-08-23 14:19:15 +01:00
|
|
|
|
|
|
|
setState(() {
|
|
|
|
userData = jsonUser;
|
|
|
|
user = jsonUser;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-21 12:28:48 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-08-22 12:28:24 +01:00
|
|
|
return SideMenu(
|
|
|
|
selectedIndex: 2,
|
|
|
|
body: Stack(
|
|
|
|
children: [
|
|
|
|
const Align(
|
|
|
|
alignment: Alignment.topLeft,
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.all(16.0),
|
|
|
|
child: Text(
|
|
|
|
'Your Profile',
|
|
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Center(
|
2024-08-23 14:19:15 +01:00
|
|
|
child: userData == null
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2024-09-10 12:32:14 +01:00
|
|
|
userData?.profilePicture != null && userData!.profilePicture.isNotEmpty ?
|
|
|
|
ClipOval(
|
|
|
|
child: Image(
|
|
|
|
image: NetworkImage(userData!.profilePicture),
|
|
|
|
height: 100,
|
|
|
|
width: 100, // Ensure width matches the height to make it fully round
|
|
|
|
fit: BoxFit.cover, // This makes sure the image fits inside the circle properly
|
|
|
|
),
|
|
|
|
)
|
2024-09-09 15:48:37 +01:00
|
|
|
: const Icon(
|
2024-08-23 14:19:15 +01:00
|
|
|
Icons.account_circle,
|
|
|
|
size: 100,
|
|
|
|
color: Colors.grey,
|
2024-09-09 15:48:37 +01:00
|
|
|
),
|
2024-08-23 14:19:15 +01:00
|
|
|
const SizedBox(height: 20),
|
|
|
|
Text(
|
|
|
|
userData!.username,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
Text(
|
|
|
|
userData!.email,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 50),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
2024-08-26 14:49:06 +01:00
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
isScrollControlled: true,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return SizedBox(
|
|
|
|
height: MediaQuery.of(context).size.height * 0.9, // 90% height
|
|
|
|
child: EditProfilePage(userData: user),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Edit'),
|
2024-08-23 14:19:15 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-08-22 12:28:24 +01:00
|
|
|
),
|
|
|
|
],
|
2024-08-21 12:28:48 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|