user_avator_widget.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/models/collection.dart';
  3. import 'package:photos/theme/colors.dart';
  4. import 'package:photos/theme/ente_theme.dart';
  5. import 'package:tuple/tuple.dart';
  6. enum AvatarType { small, mini, tiny, extra }
  7. class UserAvatarWidget extends StatelessWidget {
  8. final User user;
  9. final AvatarType type;
  10. final int currentUserID;
  11. const UserAvatarWidget(
  12. this.user, {
  13. super.key,
  14. this.currentUserID = -1,
  15. this.type = AvatarType.mini,
  16. });
  17. @override
  18. Widget build(BuildContext context) {
  19. final enteTextTheme = getEnteTextTheme(context);
  20. final colorScheme = getEnteColorScheme(context);
  21. final displayChar = (user.name == null || user.name!.isEmpty)
  22. ? user.email.substring(0, 1)
  23. : user.name!.substring(0, 1);
  24. final randomColor = colorScheme.avatarColors[
  25. (user.id ?? 0).remainder(colorScheme.avatarColors.length)];
  26. final Color decorationColor =
  27. ((user.id ?? -1) == currentUserID) ? Colors.black : randomColor;
  28. final avatarStyle = getAvatarStyle(context, type);
  29. final double size = avatarStyle.item1;
  30. final TextStyle textStyle = avatarStyle.item2;
  31. return Container(
  32. height: size,
  33. width: size,
  34. padding: const EdgeInsets.all(4),
  35. decoration: BoxDecoration(
  36. shape: BoxShape.circle,
  37. color: decorationColor,
  38. border: Border.all(
  39. color: strokeBaseDark,
  40. width: 1.0,
  41. ),
  42. ),
  43. child: CircleAvatar(
  44. backgroundColor: decorationColor,
  45. child: Text(
  46. displayChar.toUpperCase(),
  47. // fixed color
  48. style: textStyle.copyWith(color: Colors.white),
  49. ),
  50. ),
  51. );
  52. }
  53. Tuple2<double, TextStyle> getAvatarStyle(
  54. BuildContext context,
  55. AvatarType type,
  56. ) {
  57. final enteTextTheme = getEnteTextTheme(context);
  58. switch (type) {
  59. case AvatarType.small:
  60. return Tuple2(36.0, enteTextTheme.small);
  61. case AvatarType.mini:
  62. return Tuple2(24.0, enteTextTheme.mini);
  63. case AvatarType.tiny:
  64. return Tuple2(18.0, enteTextTheme.tiny);
  65. case AvatarType.extra:
  66. return Tuple2(18.0, enteTextTheme.tiny);
  67. }
  68. }
  69. }