newsfeed_card_content.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'dart:math';
  2. import 'package:auto_route/src/route/page_route_info.dart';
  3. import 'package:cpt_community/components/comments_dialog.dart';
  4. import 'package:cpt_community/modules/community/community_page.dart';
  5. import 'package:cs_resources/theme/app_colors_theme.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/widgets.dart';
  8. import 'package:plugin_basic/modules/global_web_page.dart';
  9. import 'package:plugin_basic/modules/preview_photo_page.dart';
  10. import 'package:plugin_basic/router/basic_page_router.dart';
  11. import 'package:plugin_platform/engine/image/image_preview.dart';
  12. import 'package:router/componentRouter/component_service_manager.dart';
  13. import 'package:router/ext/auto_router_extensions.dart';
  14. import 'package:shared/utils/color_utils.dart';
  15. import 'package:shared/utils/goto_page.dart';
  16. import 'package:shared/utils/log_utils.dart';
  17. import 'package:widgets/ext/ex_widget.dart';
  18. import 'package:widgets/my_load_image.dart';
  19. import 'package:widgets/my_text_view.dart';
  20. class NewsFeedCardContent extends StatelessWidget {
  21. final String content;
  22. final List<dynamic>? imageUrls;
  23. int? rowMaxImageNum=3;
  24. int? textMaxLines = 3;
  25. bool? isPreviewImage = true;
  26. NewsFeedCardContent({
  27. Key? key,
  28. required this.content,
  29. required List<dynamic> this.imageUrls,
  30. this.rowMaxImageNum = 3,
  31. this.textMaxLines = 3,
  32. this.isPreviewImage = true,
  33. }) : super(key: key);
  34. @override
  35. Widget build(BuildContext context) {
  36. List imageUrlsList = [];
  37. if(imageUrls != null && imageUrls!.isNotEmpty){
  38. // dart 取出 imageUrls 里面的前 rowMaxImageNum 张图片
  39. imageUrlsList = imageUrls!.take(rowMaxImageNum!).toList();
  40. }
  41. int totalImageCount = imageUrls!.length;
  42. int otherImageCount = imageUrls!.length - rowMaxImageNum!;
  43. int rowActualImageCount = imageUrls!.length > rowMaxImageNum! ? rowMaxImageNum! : imageUrls!.length;
  44. return LayoutBuilder(
  45. builder: (BuildContext context, BoxConstraints constraints) {
  46. final maxHeight = constraints.maxHeight;
  47. final minHeight = constraints.minHeight;
  48. final maxWidth = constraints.maxWidth;
  49. // Log.d("---maxHeight-----$maxHeight-- $minHeight $maxWidth--");
  50. return Container(
  51. width: double.infinity,
  52. // color: Colors.red,
  53. padding: const EdgeInsets.symmetric(horizontal: 15),
  54. child: Column(
  55. crossAxisAlignment: CrossAxisAlignment.start,
  56. children: [
  57. MyTextView(
  58. content,
  59. textColor: context.appColors.textBlack,
  60. fontSize: 15,
  61. maxLines: textMaxLines,
  62. isTextEllipsis: true,
  63. ),
  64. const SizedBox(height: 12),
  65. // 图片
  66. _buildImageSection(
  67. context,
  68. isPreviewImage!,
  69. maxWidth - 15,
  70. imageUrlsList,
  71. imageUrls,
  72. totalImageCount,
  73. otherImageCount,
  74. rowMaxImageNum!
  75. ),
  76. ]
  77. )
  78. );
  79. }
  80. );
  81. }
  82. Widget _buildImageSection(BuildContext context,bool isPreviewImage, double maxWidth, List? imageUrls,List? totalImageUrls, int totalImageCount, int otherImageCount, int rowMaxImageNum) {
  83. if (imageUrls != null && imageUrls!.isNotEmpty) {
  84. final imageCount = imageUrls.length;
  85. return SizedBox(
  86. width: maxWidth,
  87. height: 87,
  88. child: Row(
  89. mainAxisAlignment: MainAxisAlignment.start,
  90. children: List.generate(
  91. imageUrls.length,
  92. (index) => Container(
  93. height: 87,
  94. width: (totalImageCount < rowMaxImageNum)? maxWidth/rowMaxImageNum - 16 : maxWidth/imageCount - 16,
  95. margin: EdgeInsets.only(right: (index!=imageCount-1) ? 16 : 0 ),
  96. color: ColorUtils.string2Color("#F2F3F6"),
  97. child:Stack(
  98. children: [
  99. Hero(
  100. tag: imageUrls[index],
  101. child: MyLoadImage(
  102. imageUrls[index],
  103. height: 87,
  104. // width: maxWidth/imageCount - 16,
  105. width: (totalImageCount < rowMaxImageNum)? maxWidth/rowMaxImageNum - 16 : maxWidth/imageCount - 16,
  106. fit: BoxFit.cover,
  107. // fit:BoxFit.fitWidth,
  108. cornerRadius: 5,
  109. onClick: (){
  110. if(isPreviewImage){
  111. // 点击图片预览
  112. // 过滤掉非字符串类型的元素
  113. List<String> filteredImages = totalImageUrls?.whereType<String>().toList() ?? [];
  114. // PreviewPhotoPage.startInstance(context: context, images: filteredImages, position: index);
  115. // context.appRouter.push(PreviewPhotoPageRoute(images: filteredImages, position: index));
  116. // ImagePreviewEngine.multipleImagePreview(
  117. // context,
  118. // filteredImages,
  119. // heroes: List.generate(filteredImages.length, (index) => filteredImages[index]),
  120. // onLongPressAction: (url) {}
  121. // );
  122. GotoPage.pushPageByFade(
  123. context: context,
  124. targetPage:
  125. PreviewPhotoPage(images: filteredImages, position: index),
  126. );
  127. }
  128. },
  129. ),
  130. ),
  131. otherImageCount > 0 && index == imageCount-1 ?
  132. Positioned(
  133. bottom: 0,
  134. right: 0,
  135. child: Container(
  136. padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
  137. color: context.appColors.textBlack.withOpacity(0.5),
  138. child: MyTextView(
  139. "+$otherImageCount",
  140. textColor: Colors.white,
  141. fontSize: 12,
  142. ),
  143. ),
  144. ): const SizedBox.shrink(),
  145. ]
  146. )
  147. ),
  148. )
  149. )
  150. );
  151. } else {
  152. return const SizedBox.shrink();
  153. }
  154. }
  155. }