import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/core/app_assets.dart'; import 'package:hmg_patient_app_new/core/app_state.dart'; import 'package:hmg_patient_app_new/core/dependencies.dart'; import 'package:hmg_patient_app_new/core/utils/size_utils.dart'; class UserAvatarWidget extends StatelessWidget { final double width; final double height; final int? gender; final int? age; final BoxFit? fit; final bool isCircular; final double? borderRadius; final String? customProfileImageData; // For family members or other users const UserAvatarWidget({ Key? key, required this.width, required this.height, this.gender, this.age, this.fit, this.isCircular = false, this.borderRadius, this.customProfileImageData, }) : super(key: key); @override Widget build(BuildContext context) { final appState = getIt.get(); final userGender = gender ?? appState.getAuthenticatedUser()?.gender ?? 1; final userAge = age ?? appState.getAuthenticatedUser()?.age ?? 0; // Use custom profile image data if provided, otherwise use AppState data final profileImageData = customProfileImageData ?? appState.getProfileImageData; // Debug logging print('🖼️ UserAvatarWidget build - Has profile image: ${profileImageData != null && profileImageData.isNotEmpty}'); if (profileImageData != null && profileImageData.isNotEmpty) { print('🖼️ Profile image data length: ${profileImageData.length} characters'); } // Determine the default image based on gender and age final String defaultImage; if (userGender == 1) { // Male defaultImage = userAge < 7 ? AppAssets.babyBoyImg : AppAssets.maleImg; } else { // Female defaultImage = userAge < 7 ? AppAssets.babyGirlImg : AppAssets.femaleImg; } // Calculate border radius - default to 12.r if not specified and not circular final effectiveBorderRadius = isCircular ? width / 2 : (borderRadius ?? 12.r); // Show uploaded profile image if available if (profileImageData != null && profileImageData.isNotEmpty) { try { final bytes = base64Decode(profileImageData); final imageWidget = Image.memory( bytes, width: width, height: height, fit: fit ?? BoxFit.cover, ); return ClipRRect( borderRadius: BorderRadius.circular(effectiveBorderRadius), child: imageWidget, ); } catch (e) { // If decoding fails, show default image return ClipRRect( borderRadius: BorderRadius.circular(effectiveBorderRadius), child: Image.asset( defaultImage, width: width, height: height, fit: fit, ), ); } } // Show default image with rounded corners return ClipRRect( borderRadius: BorderRadius.circular(effectiveBorderRadius), child: Image.asset( defaultImage, width: width, height: height, fit: fit, ), ); } }