MIF_E31222854_MOBILE/lib/view/home/profile/profile.dart

219 lines
6.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:ta_tahsin/core/baseurl/base_url.dart';
import 'package:ta_tahsin/core/theme.dart';
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
Future<void> logout(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? token = prefs.getString('token');
if (token != null) {
final response = await http.post(
Uri.parse('${BaseUrl.baseUrl}/logout'),
headers: {
'Authorization': 'Bearer $token',
},
);
if (response.statusCode == 200) {
prefs.remove('token');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Berhasil logout!')),
);
context.go('/login');
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Gagal logout: ${response.body}')),
);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Token tidak ditemukan')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text(
"Profile",
),
automaticallyImplyLeading: false,
),
body: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
elevation: 5,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
gradient: LinearGradient(
colors: [
secondPrimaryColor,
Colors.blue,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
padding: const EdgeInsets.all(16),
child: Row(
children: [
CircleAvatar(
radius: 50,
backgroundColor: Colors.white,
backgroundImage: AssetImage('assets/logo/sho.jpg'),
),
const SizedBox(width: 16),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"irfan",
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.white),
),
const SizedBox(height: 5),
Text(
"083166408735",
style: TextStyle(
fontSize: 16, color: Colors.white.withOpacity(0.8)),
),
],
),
],
),
),
),
const SizedBox(height: 20),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
elevation: 5,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
gradient: LinearGradient(
colors: [
secondPrimaryColor,
Colors.blue,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildTestResultSection("Tgl Lahir", "-"),
Divider(color: Colors.white),
_buildTestResultSection("Alamat", "-"),
Divider(color: Colors.white),
_buildTestResultSection("Nama Orang Tua", "-"),
Divider(color: Colors.white),
_buildTestResultSection("email", "-"),
],
),
),
),
const SizedBox(height: 20),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: secondPrimaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
minimumSize: Size(double.infinity, 50),
),
onPressed: () {
logout(context);
},
child: Text(
"Logout",
style: TextStyle(fontSize: 16,color: whiteColor),
),
),
],
),
),
),
),
);
}
Widget _buildTestResultSection(String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(fontSize: 16, color: Colors.white),
),
SizedBox(height: 6),
Text(
value,
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
);
}
Widget _buildInfoLink(String label) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.arrow_forward, color: secondPrimaryColor),
SizedBox(width: 8),
Text(
label,
style: TextStyle(fontSize: 16, color: secondPrimaryColor),
),
],
),
);
}
}