MIF_E31230910_MP-HRIS-MOBILE/lib/widgets/atoms/custom_avatar.dart

82 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../../core/theme.dart';
class CustomAvatar extends StatelessWidget {
final String? imageUrl;
final String name;
final double size;
final Color? backgroundColor;
const CustomAvatar({
Key? key,
this.imageUrl,
required this.name,
this.size = 48.0,
this.backgroundColor,
}) : super(key: key);
String get _initials {
if (name.isEmpty) return "";
List<String> names = name.split(" ");
String initials = "";
if (names.isNotEmpty) {
initials += names[0][0];
if (names.length > 1) {
initials += names[1][0];
}
} else {
initials = name[0];
}
return initials.toUpperCase();
}
@override
Widget build(BuildContext context) {
return Container(
width: size,
height: size,
decoration: BoxDecoration(
color: backgroundColor ?? AppTheme.primaryBlue.withOpacity(0.1),
shape: BoxShape.circle,
),
child: ClipOval(
child: imageUrl != null && imageUrl!.isNotEmpty
? CachedNetworkImage(
imageUrl: imageUrl!,
fit: BoxFit.cover,
width: size,
height: size,
placeholder: (context, url) => Center(
child: SizedBox(
width: size * 0.4,
height: size * 0.4,
child: const CircularProgressIndicator(strokeWidth: 2),
),
),
errorWidget: (context, url, error) => Center(
child: Text(
_initials,
style: TextStyle(
color: AppTheme.primaryBlue,
fontWeight: FontWeight.bold,
fontSize: size * 0.4,
),
),
),
)
: Center(
child: Text(
_initials,
style: TextStyle(
color: AppTheme.primaryBlue,
fontWeight: FontWeight.bold,
fontSize: size * 0.4,
),
),
),
),
);
}
}