track_overlapping_avatars.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'package:flutter/material.dart';
  2. import 'package:location/resource/colors.gen.dart';
  3. class OverlappingAvatars extends StatelessWidget {
  4. final List<String> avatarUrls;
  5. final double size;
  6. final double overlap;
  7. final Color borderColor;
  8. final double borderWidth;
  9. final int maxDisplay;
  10. final String moreText;
  11. final TextStyle moreTextStyle;
  12. const OverlappingAvatars({
  13. Key? key,
  14. required this.avatarUrls,
  15. this.size = 40.0,
  16. this.overlap = 0.2,
  17. this.borderColor = Colors.white,
  18. this.borderWidth = 2.0,
  19. this.maxDisplay = 4,
  20. this.moreText = '+%d',
  21. this.moreTextStyle = const TextStyle(
  22. color: Colors.white,
  23. fontWeight: FontWeight.bold,
  24. fontSize: 12,
  25. ),
  26. }) : super(key: key);
  27. @override
  28. Widget build(BuildContext context) {
  29. final displayCount = avatarUrls.length > maxDisplay
  30. ? maxDisplay
  31. : avatarUrls.length;
  32. final remainingCount = avatarUrls.length - displayCount;
  33. final widgets = <Widget>[];
  34. for (var i = 0; i < displayCount; i++) {
  35. widgets.add(
  36. Container(
  37. width: size,
  38. height: size,
  39. decoration: BoxDecoration(
  40. shape: BoxShape.circle,
  41. border: Border.all(
  42. color: borderColor,
  43. width: borderWidth,
  44. ),
  45. ),
  46. child: CircleAvatar(
  47. backgroundImage: NetworkImage(avatarUrls[i]),
  48. ),
  49. ),
  50. );
  51. }
  52. if (remainingCount > 0) {
  53. widgets.add(
  54. Container(
  55. width: size,
  56. height: size,
  57. decoration: BoxDecoration(
  58. shape: BoxShape.circle,
  59. color: Colors.grey[300],
  60. border: Border.all(
  61. color: ColorName.white,//borderColor,
  62. width: borderWidth,
  63. ),
  64. ),
  65. child: Center(
  66. child: Text(
  67. moreText.replaceAll('%d', remainingCount.toString()),
  68. style: moreTextStyle,
  69. ),
  70. ),
  71. ),
  72. );
  73. }
  74. return Stack(
  75. children: List.generate(widgets.length, (index) {
  76. return Positioned(
  77. left: (size * overlap) * index,
  78. child: widgets[index],
  79. );
  80. }),
  81. );
  82. }
  83. }