Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.5 KiB

  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:path/path.dart';
  4. enum AvatarTypes {
  5. initials,
  6. icon,
  7. image,
  8. }
  9. class CustomCircleAvatar extends StatelessWidget {
  10. final String? initials;
  11. final Icon? icon;
  12. final File? image;
  13. final Function ()? editImageCallback;
  14. final double radius;
  15. const CustomCircleAvatar({
  16. Key? key,
  17. this.initials,
  18. this.icon,
  19. this.image,
  20. this.editImageCallback,
  21. this.radius = 20,
  22. }) : super(key: key);
  23. Widget avatar(BuildContext context) {
  24. AvatarTypes? type;
  25. if (icon != null) {
  26. type = AvatarTypes.icon;
  27. }
  28. if (initials != null) {
  29. type = AvatarTypes.initials;
  30. }
  31. if (image != null) {
  32. type = AvatarTypes.image;
  33. }
  34. if (type == null) {
  35. throw ArgumentError('Invalid arguments passed to CustomCircleAvatar');
  36. }
  37. if (type == AvatarTypes.initials) {
  38. return CircleAvatar(
  39. backgroundColor: Theme.of(context).colorScheme.tertiary,
  40. radius: radius,
  41. child: Text(initials!),
  42. );
  43. }
  44. if (type == AvatarTypes.icon) {
  45. return CircleAvatar(
  46. backgroundColor: Theme.of(context).colorScheme.tertiary,
  47. radius: radius,
  48. child: icon,
  49. );
  50. }
  51. return Container(
  52. width: radius * 2,
  53. height: radius * 2,
  54. decoration: BoxDecoration(
  55. shape: BoxShape.circle,
  56. image: DecorationImage(
  57. image: Image.file(image!).image,
  58. fit: BoxFit.fill
  59. ),
  60. ),
  61. );
  62. }
  63. Widget editIcon(BuildContext context) {
  64. if (editImageCallback == null) {
  65. return const SizedBox.shrink();
  66. }
  67. return SizedBox(
  68. height: (radius * 2),
  69. width: (radius * 2),
  70. child: Align(
  71. alignment: Alignment.bottomRight,
  72. child: GestureDetector(
  73. onTap: editImageCallback,
  74. child: Container(
  75. height: (radius / 2) + (radius / 7),
  76. width: (radius / 2) + (radius / 7),
  77. decoration: BoxDecoration(
  78. color: Theme.of(context).scaffoldBackgroundColor,
  79. borderRadius: BorderRadius.circular(30),
  80. ),
  81. child: Icon(
  82. Icons.add,
  83. color: Theme.of(context).primaryColor,
  84. size: radius / 2
  85. ),
  86. ),
  87. ),
  88. ),
  89. );
  90. }
  91. @override
  92. Widget build(BuildContext context) {
  93. return Stack(
  94. children: [
  95. avatar(context),
  96. editIcon(context),
  97. ]
  98. );
  99. }
  100. }