user_circle_avatar.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. bool useRandomBackgroundColor;
  14. UserCircleAvatar({
  15. super.key,
  16. this.radius = 22,
  17. this.size = 44,
  18. this.useRandomBackgroundColor = false,
  19. required this.user,
  20. });
  21. @override
  22. Widget build(BuildContext context, WidgetRef ref) {
  23. final randomColors = [
  24. Colors.red[200],
  25. Colors.blue[200],
  26. Colors.green[200],
  27. Colors.yellow[200],
  28. Colors.purple[200],
  29. Colors.orange[200],
  30. Colors.pink[200],
  31. Colors.teal[200],
  32. Colors.indigo[200],
  33. Colors.cyan[200],
  34. Colors.brown[200],
  35. ];
  36. final profileImageUrl =
  37. '${Store.get(StoreKey.serverEndpoint)}/user/profile-image/${user.id}?d=${Random().nextInt(1024)}';
  38. return CircleAvatar(
  39. backgroundColor: useRandomBackgroundColor
  40. ? randomColors[Random().nextInt(randomColors.length)]
  41. : Theme.of(context).primaryColor,
  42. radius: radius,
  43. child: user.profileImagePath == ""
  44. ? Text(
  45. user.firstName[0].toUpperCase(),
  46. style: const TextStyle(
  47. fontWeight: FontWeight.bold,
  48. color: Colors.black,
  49. ),
  50. )
  51. : ClipRRect(
  52. borderRadius: BorderRadius.circular(50),
  53. child: CachedNetworkImage(
  54. fit: BoxFit.cover,
  55. cacheKey: user.profileImagePath,
  56. width: size,
  57. height: size,
  58. placeholder: (_, __) => Image.memory(kTransparentImage),
  59. imageUrl: profileImageUrl,
  60. httpHeaders: {
  61. "Authorization": "Bearer ${Store.get(StoreKey.accessToken)}",
  62. },
  63. fadeInDuration: const Duration(milliseconds: 300),
  64. errorWidget: (context, error, stackTrace) =>
  65. Image.memory(kTransparentImage),
  66. ),
  67. ),
  68. );
  69. }
  70. }