user_circle_avatar.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'dart:math';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:hooks_riverpod/hooks_riverpod.dart';
  5. import 'package:immich_mobile/shared/models/store.dart';
  6. import 'package:immich_mobile/shared/models/user.dart';
  7. import 'package:immich_mobile/shared/ui/transparent_image.dart';
  8. // ignore: must_be_immutable
  9. class UserCircleAvatar extends ConsumerWidget {
  10. final User user;
  11. double radius;
  12. double size;
  13. UserCircleAvatar({
  14. super.key,
  15. this.radius = 22,
  16. this.size = 44,
  17. required this.user,
  18. });
  19. @override
  20. Widget build(BuildContext context, WidgetRef ref) {
  21. bool isDarkTheme = Theme.of(context).brightness == Brightness.dark;
  22. final profileImageUrl =
  23. '${Store.get(StoreKey.serverEndpoint)}/user/profile-image/${user.id}?d=${Random().nextInt(1024)}';
  24. final textIcon = Text(
  25. user.name[0].toUpperCase(),
  26. style: TextStyle(
  27. fontWeight: FontWeight.bold,
  28. fontSize: 12,
  29. color: isDarkTheme && user.avatarColor == AvatarColorEnum.primary
  30. ? Colors.black
  31. : Colors.white,
  32. ),
  33. );
  34. return CircleAvatar(
  35. backgroundColor: user.avatarColor.toColor(),
  36. radius: radius,
  37. child: user.profileImagePath.isEmpty
  38. ? textIcon
  39. : ClipRRect(
  40. borderRadius: BorderRadius.circular(50),
  41. child: CachedNetworkImage(
  42. fit: BoxFit.cover,
  43. cacheKey: user.profileImagePath,
  44. width: size,
  45. height: size,
  46. placeholder: (_, __) => Image.memory(kTransparentImage),
  47. imageUrl: profileImageUrl,
  48. httpHeaders: {
  49. "Authorization": "Bearer ${Store.get(StoreKey.accessToken)}",
  50. },
  51. fadeInDuration: const Duration(milliseconds: 300),
  52. errorWidget: (context, error, stackTrace) => textIcon,
  53. ),
  54. ),
  55. );
  56. }
  57. }