user_avator_widget.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. Color decorationColor;
  26. if (user.id == null || user.id! <= 0 || user.id == currentUserID) {
  27. decorationColor = Colors.black;
  28. } else {
  29. decorationColor = colorScheme
  30. .avatarColors[(user.id!).remainder(colorScheme.avatarColors.length)];
  31. }
  32. final avatarStyle = getAvatarStyle(context, type);
  33. final double size = avatarStyle.item1;
  34. final TextStyle textStyle = avatarStyle.item2;
  35. return Container(
  36. height: size,
  37. width: size,
  38. padding: thumbnailView
  39. ? const EdgeInsets.only(bottom: 1)
  40. : const EdgeInsets.all(2),
  41. decoration: thumbnailView
  42. ? null
  43. : BoxDecoration(
  44. shape: BoxShape.circle,
  45. color: decorationColor,
  46. border: Border.all(
  47. color: strokeBaseDark,
  48. width: 1.0,
  49. ),
  50. ),
  51. child: CircleAvatar(
  52. backgroundColor: decorationColor,
  53. child: Text(
  54. displayChar.toUpperCase(),
  55. // fixed color
  56. style: textStyle.copyWith(color: Colors.white),
  57. ),
  58. ),
  59. );
  60. }
  61. Tuple2<double, TextStyle> getAvatarStyle(
  62. BuildContext context,
  63. AvatarType type,
  64. ) {
  65. final enteTextTheme = getEnteTextTheme(context);
  66. switch (type) {
  67. case AvatarType.small:
  68. return Tuple2(36.0, enteTextTheme.small);
  69. case AvatarType.mini:
  70. return Tuple2(24.0, enteTextTheme.mini);
  71. case AvatarType.tiny:
  72. return Tuple2(18.0, enteTextTheme.tiny);
  73. case AvatarType.extra:
  74. return Tuple2(18.0, enteTextTheme.tiny);
  75. }
  76. }
  77. }