HydroNutrify_Ari_e41221567/lib/ui/shared/profile_header.dart

121 lines
3.4 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:mobile_monitoring/core/constants/constants.dart';
class ProfileHeader extends StatelessWidget {
final String? displayName;
final String? email;
final File? selectedImage;
final String? photoUrl;
final VoidCallback onCameraPressed;
const ProfileHeader({
super.key,
required this.displayName,
required this.email,
required this.onCameraPressed,
this.selectedImage,
this.photoUrl,
});
String _getInitials(String? name) {
if (name == null || name.isEmpty) return 'U';
final parts = name.trim().split(' ');
if (parts.length >= 2) {
return '${parts[0][0]}${parts[1][0]}'.toUpperCase();
}
return name[0].toUpperCase();
}
ImageProvider? _buildImageProvider() {
if (selectedImage != null) {
return FileImage(selectedImage!);
}
if (photoUrl == null || photoUrl!.isEmpty) {
return null;
}
if (photoUrl!.startsWith('http://') || photoUrl!.startsWith('https://')) {
return NetworkImage(photoUrl!);
}
return FileImage(File(photoUrl!));
}
@override
Widget build(BuildContext context) {
final avatarProvider = _buildImageProvider();
return Container(
width: double.infinity,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [AppColors.secondaryLight, AppColors.secondaryDark],
),
),
padding: const EdgeInsets.symmetric(vertical: 40),
child: Column(
children: [
// Profile Avatar
Stack(
children: [
CircleAvatar(
radius: 60,
backgroundColor: AppColors.white,
child: CircleAvatar(
radius: 55,
backgroundColor: AppColors.secondaryLight,
backgroundImage: avatarProvider,
child: avatarProvider == null
? Text(
_getInitials(displayName),
style: const TextStyle(
fontWeight: FontWeight.bold,
color: AppColors.white,
),
)
: null,
),
),
Positioned(
bottom: 0,
right: 0,
child: Container(
decoration: BoxDecoration(
color: AppColors.primaryDark,
shape: BoxShape.circle,
border: Border.all(color: AppColors.white, width: 3),
),
child: IconButton(
icon: const Icon(Icons.camera_alt, size: 20),
color: AppColors.white,
onPressed: onCameraPressed,
),
),
),
],
),
const SizedBox(height: 16),
Text(
displayName ?? 'Nama Pengguna',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
email ?? '',
style: TextStyle(
fontSize: 16,
color: Colors.white.withValues(alpha: 0.9),
),
),
],
),
);
}
}