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.

79 lines
1.6 KiB

  1. import 'package:flutter/material.dart';
  2. enum AvatarTypes {
  3. initials,
  4. icon,
  5. image,
  6. }
  7. class CustomCircleAvatar extends StatefulWidget {
  8. final String? initials;
  9. final Icon? icon;
  10. final String? imagePath;
  11. final double radius;
  12. const CustomCircleAvatar({
  13. Key? key,
  14. this.initials,
  15. this.icon,
  16. this.imagePath,
  17. this.radius = 20,
  18. }) : super(key: key);
  19. @override
  20. _CustomCircleAvatarState createState() => _CustomCircleAvatarState();
  21. }
  22. class _CustomCircleAvatarState extends State<CustomCircleAvatar>{
  23. AvatarTypes type = AvatarTypes.image;
  24. @override
  25. void initState() {
  26. super.initState();
  27. if (widget.imagePath != null) {
  28. type = AvatarTypes.image;
  29. return;
  30. }
  31. if (widget.icon != null) {
  32. type = AvatarTypes.icon;
  33. return;
  34. }
  35. if (widget.initials != null) {
  36. type = AvatarTypes.initials;
  37. return;
  38. }
  39. throw ArgumentError('Invalid arguments passed to CustomCircleAvatar');
  40. }
  41. Widget avatar() {
  42. if (type == AvatarTypes.initials) {
  43. return CircleAvatar(
  44. backgroundColor: Colors.grey[300],
  45. child: Text(widget.initials!),
  46. radius: widget.radius,
  47. );
  48. }
  49. if (type == AvatarTypes.icon) {
  50. return CircleAvatar(
  51. backgroundColor: Colors.grey[300],
  52. child: widget.icon,
  53. radius: widget.radius,
  54. );
  55. }
  56. return CircleAvatar(
  57. backgroundImage: AssetImage(widget.imagePath!),
  58. radius: widget.radius,
  59. );
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return avatar();
  64. }
  65. }