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.

114 lines
2.4 KiB

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