You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_Patient_App_New/lib/widgets/user_avatar_widget.dart

94 lines
2.8 KiB
Dart

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<AppState>();
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;
// 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,
),
);
}
}