This commit is contained in:
BakoL2323 2026-04-17 11:36:58 +07:00
parent c90c5356e5
commit 1ff6fc584e
4 changed files with 180 additions and 145 deletions

View File

@ -1,2 +1,2 @@
const String baseUrl = 'http://192.168.1.6:8000/api'; const String baseUrl = 'http://192.168.0.118:8000/api';
const String baseImageurl = 'http://192.168.1.6:8000/'; const String baseImageurl = 'http://192.168.0.118:8000/';

View File

@ -55,7 +55,7 @@ class _DashboardPageState extends State<DashboardPage> {
} }
final response = await http.get( final response = await http.get(
Uri.parse('http://192.168.1.6:8000/api/dashboard/$userId'), Uri.parse('http://192.168.0.118:8000/api/dashboard/$userId'),
); );
print("STATUS: ${response.statusCode}"); print("STATUS: ${response.statusCode}");

View File

@ -20,7 +20,7 @@ class _HistoryPageState extends State<HistoryPage> {
bool isLoading = true; bool isLoading = true;
final String apiUrl = "http://192.168.1.6:8000/api/laporan"; final String apiUrl = "http://192.168.0.118:8000/api/laporan";
@override @override
void initState() { void initState() {

View File

@ -24,23 +24,33 @@ class _ProfilePageState extends State<ProfilePage> {
// GET PROFILE // GET PROFILE
// ========================== // ==========================
Future getProfile() async { Future getProfile() async {
final response = await http.get( try {
Uri.parse("http://192.168.1.6:8000/api/profile"), final response = await http.get(
headers: {"Accept": "application/json"}, Uri.parse("http://192.168.0.118:8000/api/profile?user_id=4"),
); headers: {"Accept": "application/json"},
);
print(response.statusCode); print(response.statusCode);
print(response.body); print(response.body);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final data = jsonDecode(response.body); final data = jsonDecode(response.body);
setState(() {
user = data['user'];
isLoading = false;
});
} else {
setState(() {
isLoading = false;
});
print("Error API");
}
} catch (e) {
setState(() { setState(() {
user = data['user'];
isLoading = false; isLoading = false;
}); });
} else { print("Error: $e");
print("Error API");
} }
} }
@ -48,7 +58,8 @@ class _ProfilePageState extends State<ProfilePage> {
// PICK IMAGE // PICK IMAGE
// ========================== // ==========================
Future<void> _pickImage() async { Future<void> _pickImage() async {
final XFile? picked = await _picker.pickImage(source: ImageSource.gallery); final XFile? picked =
await _picker.pickImage(source: ImageSource.gallery);
if (picked != null) { if (picked != null) {
setState(() { setState(() {
@ -63,26 +74,49 @@ class _ProfilePageState extends State<ProfilePage> {
// UPLOAD PHOTO // UPLOAD PHOTO
// ========================== // ==========================
Future uploadPhoto() async { Future uploadPhoto() async {
var request = http.MultipartRequest( try {
"POST",
Uri.parse("http://192.168.1.6:8000/api/upload-photo"),
);
request.headers['Accept'] = 'application/json';
request.fields['user_id'] = user!['id'].toString();
request.files.add(
await http.MultipartFile.fromPath('profile_photo', _imageFile!.path),
);
var response = await request.send();
if (response.statusCode == 200) {
setState(() { setState(() {
_imageFile = null; // reset biar ambil dari server isLoading = true;
}); });
getProfile();
var request = http.MultipartRequest(
"POST",
Uri.parse("http://192.168.0.118:8000/api/upload-photo"),
);
request.headers['Accept'] = 'application/json';
request.fields['user_id'] = user?['id'].toString() ?? "0";
request.files.add(
await http.MultipartFile.fromPath(
'profile_photo',
_imageFile!.path,
),
);
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
print(response.statusCode);
print(response.body);
if (response.statusCode == 200) {
setState(() {
_imageFile = null;
});
await getProfile(); // refresh
} else {
setState(() {
isLoading = false;
});
print("Upload gagal");
}
} catch (e) {
setState(() {
isLoading = false;
});
print("Error upload: $e");
} }
} }
@ -92,131 +126,132 @@ class _ProfilePageState extends State<ProfilePage> {
getProfile(); getProfile();
} }
// 🔥 Tambahan supaya refresh saat balik ke halaman ini
@override
void didChangeDependencies() {
super.didChangeDependencies();
getProfile();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (isLoading) { // 🔥 ANTI CRASH
return const Center(child: CircularProgressIndicator()); if (isLoading || user == null) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
} }
return SingleChildScrollView( return Scaffold(
padding: const EdgeInsets.all(16), body: SingleChildScrollView(
child: Column( padding: const EdgeInsets.all(16),
children: [ child: Column(
// ====================== children: [
// HEADER PROFILE // ======================
// ====================== // HEADER PROFILE
Container( // ======================
padding: const EdgeInsets.all(20), Container(
decoration: BoxDecoration( padding: const EdgeInsets.all(20),
gradient: const LinearGradient( decoration: BoxDecoration(
colors: [AppColors.button, AppColors.button], gradient: const LinearGradient(
colors: [AppColors.button, AppColors.button],
),
borderRadius: BorderRadius.circular(16),
), ),
borderRadius: BorderRadius.circular(16), child: Column(
), children: [
child: Column( // PHOTO
children: [ CircleAvatar(
// PHOTO radius: 60,
CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
backgroundImage: _imageFile != null
? FileImage(File(_imageFile!.path))
: user!['photo_url'] != null
// 🔥 CACHE BUSTER DI SINI
? NetworkImage(
user!['photo_url'] +
"?t=${DateTime.now().millisecondsSinceEpoch}",
)
: null,
child: user!['photo_url'] == null && _imageFile == null
? const Icon(Icons.person, size: 60)
: null,
),
const SizedBox(height: 12),
Text(
user!['name'],
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
user!['role'],
style: const TextStyle(color: Colors.white70),
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _pickImage,
icon: const Icon(Icons.camera_alt),
label: const Text("Ubah Foto"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white, backgroundColor: Colors.white,
foregroundColor: const Color(0xFF206E97), backgroundImage: _imageFile != null
? FileImage(File(_imageFile!.path))
: (user?['photo_url'] != null
? NetworkImage(
user!['photo_url'] +
"?t=${DateTime.now().millisecondsSinceEpoch}",
)
: null),
child: (user?['photo_url'] == null &&
_imageFile == null)
? const Icon(Icons.person, size: 60)
: null,
), ),
),
], const SizedBox(height: 12),
Text(
user?['name'] ?? "-",
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
user?['role'] ?? "-",
style: const TextStyle(color: Colors.white70),
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _pickImage,
icon: const Icon(Icons.camera_alt),
label: const Text("Ubah Foto"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: const Color(0xFF206E97),
),
),
],
),
), ),
),
const SizedBox(height: 20), const SizedBox(height: 20),
// ====================== // ======================
// DETAIL PROFILE // DETAIL PROFILE
// ====================== // ======================
Card( Card(
child: Column( child: Column(
children: [ children: [
ListTile( ListTile(
leading: const Icon(Icons.email), leading: const Icon(Icons.email),
title: const Text("Email"), title: const Text("Email"),
subtitle: Text(user!['email']), subtitle: Text(user?['email'] ?? "-"),
), ),
const Divider(),
const Divider(), ListTile(
leading: const Icon(Icons.home),
ListTile( title: const Text("Alamat"),
leading: const Icon(Icons.home), subtitle: Text(user?['address'] ?? "-"),
title: const Text("Alamat"), ),
subtitle: Text(user!['address'] ?? "-"), ],
), ),
],
), ),
),
const SizedBox(height: 20), const SizedBox(height: 20),
// ====================== // ======================
// LOGOUT // LOGOUT
// ====================== // ======================
Card( Card(
child: ListTile( child: ListTile(
leading: const Icon(Icons.logout, color: Colors.red), leading: const Icon(Icons.logout, color: Colors.red),
title: const Text("Logout", style: TextStyle(color: Colors.red)), title: const Text(
onTap: () { "Logout",
Navigator.pushAndRemoveUntil( style: TextStyle(color: Colors.red),
context, ),
MaterialPageRoute(builder: (context) => const LoginPage()), onTap: () {
(route) => false, Navigator.pushAndRemoveUntil(
); context,
}, MaterialPageRoute(
builder: (context) => const LoginPage(),
),
(route) => false,
);
},
),
), ),
), ],
], ),
), ),
); );
} }