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 baseImageurl = 'http://192.168.1.6:8000/';
const String baseUrl = 'http://192.168.0.118:8000/api';
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(
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}");

View File

@ -20,7 +20,7 @@ class _HistoryPageState extends State<HistoryPage> {
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
void initState() {

View File

@ -24,8 +24,9 @@ class _ProfilePageState extends State<ProfilePage> {
// GET PROFILE
// ==========================
Future getProfile() async {
try {
final response = await http.get(
Uri.parse("http://192.168.1.6:8000/api/profile"),
Uri.parse("http://192.168.0.118:8000/api/profile?user_id=4"),
headers: {"Accept": "application/json"},
);
@ -40,15 +41,25 @@ class _ProfilePageState extends State<ProfilePage> {
isLoading = false;
});
} else {
setState(() {
isLoading = false;
});
print("Error API");
}
} catch (e) {
setState(() {
isLoading = false;
});
print("Error: $e");
}
}
// ==========================
// PICK IMAGE
// ==========================
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) {
setState(() {
@ -63,26 +74,49 @@ class _ProfilePageState extends State<ProfilePage> {
// UPLOAD PHOTO
// ==========================
Future uploadPhoto() async {
try {
setState(() {
isLoading = true;
});
var request = http.MultipartRequest(
"POST",
Uri.parse("http://192.168.1.6:8000/api/upload-photo"),
Uri.parse("http://192.168.0.118:8000/api/upload-photo"),
);
request.headers['Accept'] = 'application/json';
request.fields['user_id'] = user!['id'].toString();
request.fields['user_id'] = user?['id'].toString() ?? "0";
request.files.add(
await http.MultipartFile.fromPath('profile_photo', _imageFile!.path),
await http.MultipartFile.fromPath(
'profile_photo',
_imageFile!.path,
),
);
var response = await request.send();
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; // reset biar ambil dari server
_imageFile = null;
});
getProfile();
await getProfile(); // refresh
} else {
setState(() {
isLoading = false;
});
print("Upload gagal");
}
} catch (e) {
setState(() {
isLoading = false;
});
print("Error upload: $e");
}
}
@ -92,20 +126,17 @@ class _ProfilePageState extends State<ProfilePage> {
getProfile();
}
// 🔥 Tambahan supaya refresh saat balik ke halaman ini
@override
void didChangeDependencies() {
super.didChangeDependencies();
getProfile();
}
@override
Widget build(BuildContext context) {
if (isLoading) {
return const Center(child: CircularProgressIndicator());
// 🔥 ANTI CRASH
if (isLoading || user == null) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return SingleChildScrollView(
return Scaffold(
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: [
@ -128,14 +159,14 @@ class _ProfilePageState extends State<ProfilePage> {
backgroundColor: Colors.white,
backgroundImage: _imageFile != null
? FileImage(File(_imageFile!.path))
: user!['photo_url'] != null
// 🔥 CACHE BUSTER DI SINI
: (user?['photo_url'] != null
? NetworkImage(
user!['photo_url'] +
"?t=${DateTime.now().millisecondsSinceEpoch}",
)
: null,
child: user!['photo_url'] == null && _imageFile == null
: null),
child: (user?['photo_url'] == null &&
_imageFile == null)
? const Icon(Icons.person, size: 60)
: null,
),
@ -143,7 +174,7 @@ class _ProfilePageState extends State<ProfilePage> {
const SizedBox(height: 12),
Text(
user!['name'],
user?['name'] ?? "-",
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
@ -154,7 +185,7 @@ class _ProfilePageState extends State<ProfilePage> {
const SizedBox(height: 4),
Text(
user!['role'],
user?['role'] ?? "-",
style: const TextStyle(color: Colors.white70),
),
@ -184,15 +215,13 @@ class _ProfilePageState extends State<ProfilePage> {
ListTile(
leading: const Icon(Icons.email),
title: const Text("Email"),
subtitle: Text(user!['email']),
subtitle: Text(user?['email'] ?? "-"),
),
const Divider(),
ListTile(
leading: const Icon(Icons.home),
title: const Text("Alamat"),
subtitle: Text(user!['address'] ?? "-"),
subtitle: Text(user?['address'] ?? "-"),
),
],
),
@ -206,11 +235,16 @@ class _ProfilePageState extends State<ProfilePage> {
Card(
child: ListTile(
leading: const Icon(Icons.logout, color: Colors.red),
title: const Text("Logout", style: TextStyle(color: Colors.red)),
title: const Text(
"Logout",
style: TextStyle(color: Colors.red),
),
onTap: () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const LoginPage()),
MaterialPageRoute(
builder: (context) => const LoginPage(),
),
(route) => false,
);
},
@ -218,6 +252,7 @@ class _ProfilePageState extends State<ProfilePage> {
),
],
),
),
);
}
}