immich/mobile/lib/widgets/common/user_circle_avatar.dart
2026-08-06 02:14:44 +05:30

59 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/domain/models/user.model.dart';
import 'package:immich_mobile/presentation/widgets/images/remote_image_provider.dart';
import 'package:immich_mobile/providers/infrastructure/session.provider.dart';
class UserCircleAvatar extends ConsumerWidget {
final UserDto user;
final double size;
final bool hasBorder;
final double opacity;
const UserCircleAvatar({super.key, this.size = 44, this.hasBorder = false, this.opacity = 1, required this.user});
@override
Widget build(BuildContext context, WidgetRef ref) {
final userAvatarColor = user.avatarColor.toColor().withValues(alpha: opacity);
final profileImageUrl =
'${ref.watch(sessionProvider.select((s) => s.serverEndpoint))!}/users/${user.id}/profile-image?d=${user.profileChangedAt.millisecondsSinceEpoch}';
final textColor = (user.avatarColor.toColor().computeLuminance() > 0.5 ? Colors.black : Colors.white).withValues(
alpha: opacity,
);
final textIcon = DefaultTextStyle(
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 12, color: textColor),
child: Text(user.name[0].toUpperCase()),
);
return Tooltip(
message: user.name,
child: UnconstrainedBox(
child: Container(
width: size,
height: size,
decoration: BoxDecoration(
color: userAvatarColor,
shape: BoxShape.circle,
border: hasBorder ? Border.all(color: userAvatarColor.withValues(alpha: opacity), width: 1.5) : null,
),
child: user.hasProfileImage
? ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(size / 2)),
child: Image(
fit: BoxFit.cover,
width: size,
height: size,
image: RemoteImageProvider(url: profileImageUrl),
errorBuilder: (context, error, stackTrace) => textIcon,
color: Colors.white.withValues(alpha: opacity),
colorBlendMode: BlendMode.modulate,
),
)
: Center(child: textIcon),
),
),
);
}
}