user_avator_widget.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. final bool thumbnailView;
  12. const UserAvatarWidget(
  13. this.user, {
  14. super.key,
  15. this.currentUserID = -1,
  16. this.type = AvatarType.mini,
  17. this.thumbnailView = false,
  18. });
  19. @override
  20. Widget build(BuildContext context) {
  21. final colorScheme = getEnteColorScheme(context);
  22. final displayChar = (user.name == null || user.name!.isEmpty)
  23. ? ((user.email.isEmpty) ? " " : user.email.substring(0, 1))
  24. : user.name!.substring(0, 1);
  25. final randomColor = colorScheme.avatarColors[
  26. (user.id ?? 0).remainder(colorScheme.avatarColors.length)];
  27. final Color decorationColor =
  28. ((user.id ?? -1) == currentUserID) ? Colors.black : randomColor;
  29. final avatarStyle = getAvatarStyle(context, type);
  30. final double size = avatarStyle.item1;
  31. final TextStyle textStyle = avatarStyle.item2;
  32. return Container(
  33. height: size,
  34. width: size,
  35. padding: thumbnailView
  36. ? const EdgeInsets.only(bottom: 1)
  37. : const EdgeInsets.all(2),
  38. decoration: thumbnailView
  39. ? null
  40. : BoxDecoration(
  41. shape: BoxShape.circle,
  42. color: decorationColor,
  43. border: Border.all(
  44. color: strokeBaseDark,
  45. width: 1.0,
  46. ),
  47. ),
  48. child: CircleAvatar(
  49. backgroundColor: decorationColor,
  50. child: Text(
  51. displayChar.toUpperCase(),
  52. // fixed color
  53. style: textStyle.copyWith(color: Colors.white),
  54. ),
  55. ),
  56. );
  57. }
  58. Tuple2<double, TextStyle> getAvatarStyle(
  59. BuildContext context,
  60. AvatarType type,
  61. ) {
  62. final enteTextTheme = getEnteTextTheme(context);
  63. switch (type) {
  64. case AvatarType.small:
  65. return Tuple2(36.0, enteTextTheme.small);
  66. case AvatarType.mini:
  67. return Tuple2(24.0, enteTextTheme.mini);
  68. case AvatarType.tiny:
  69. return Tuple2(18.0, enteTextTheme.tiny);
  70. case AvatarType.extra:
  71. return Tuple2(18.0, enteTextTheme.tiny);
  72. }
  73. }
  74. }