post_container.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
  3. import 'package:url_launcher/url_launcher.dart';
  4. import 'package:veloe_kemono_party_flutter/models/attachment.dart';
  5. import 'package:veloe_kemono_party_flutter/pages/media_carousel_screen';
  6. import 'package:veloe_kemono_party_flutter/pages/widgets/video_preview.dart';
  7. import 'smart_image_container.dart';
  8. import 'package:share_plus/share_plus.dart';
  9. import '../../models/post.dart';
  10. class PostContainer extends StatelessWidget {
  11. final Post post;
  12. final int postIndex; // The index of this post in the list of posts (in PostsScreen)
  13. final List<Post> allPosts;
  14. final Future<void> Function() loadMorePosts;
  15. const PostContainer({
  16. super.key,
  17. required this.post,
  18. required this.postIndex,
  19. required this.allPosts,
  20. required this.loadMorePosts,
  21. });
  22. @override
  23. Widget build(BuildContext context) {
  24. return Card(
  25. margin: const EdgeInsets.all(8),
  26. child: Padding(
  27. padding: const EdgeInsets.all(16),
  28. child: Column(
  29. mainAxisSize: MainAxisSize.min,
  30. crossAxisAlignment: CrossAxisAlignment.start,
  31. children: [
  32. _PostHeader(post: post),
  33. _buildContent(context, post.mediaAttachments),
  34. //_PostContentSection(post: post),
  35. _PostActionsFooter(post: post),
  36. ],
  37. ),
  38. ),
  39. );
  40. }
  41. Widget _buildContent(BuildContext context, List<Attachment> attachments) {
  42. final mediaAttachments = attachments.where((a) => a.isImage || a.isVideo).toList();
  43. return Column(
  44. mainAxisSize: MainAxisSize.min,
  45. crossAxisAlignment: CrossAxisAlignment.start,
  46. children: [
  47. // Text Content
  48. HtmlWidget(
  49. post.content,
  50. textStyle: Theme.of(context).textTheme.bodyMedium,
  51. onTapUrl: (url) => _handleUrlLaunch(context, url),
  52. ),
  53. if (mediaAttachments.isNotEmpty)
  54. LayoutBuilder(
  55. builder: (context, constraints) {
  56. return Column(
  57. children: [
  58. const SizedBox(height: 12),
  59. GridView.builder(
  60. shrinkWrap: true,
  61. physics: const NeverScrollableScrollPhysics(),
  62. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  63. crossAxisCount: _calculateColumnCount(constraints.maxWidth),
  64. ),
  65. itemCount: mediaAttachments.length,
  66. itemBuilder: (context, index) => Padding(
  67. padding: const EdgeInsets.all(4),
  68. child:
  69. _buildMediaPreview(context, mediaAttachments[index], index, mediaAttachments)
  70. /*SmartImageContainer(
  71. imageUrl: imageAttachments[index].link,
  72. //onTap: () => _handleAttachmentTap(index),
  73. ),*/
  74. ),
  75. ),
  76. // Attachment links list
  77. ...post.mediaAttachments.map(
  78. (attachment) => _buildAttachmentLink(context,attachment),
  79. ),
  80. ],
  81. );
  82. },
  83. ),
  84. /*
  85. LayoutBuilder(
  86. builder: (context, constraints) {
  87. final itemWidth = constraints.maxWidth;
  88. return Column(
  89. children: [
  90. const SizedBox(height: 12),
  91. Wrap(
  92. spacing: 8, // горизонтальный отступ между элементами
  93. runSpacing: 8, // вертикальный отступ
  94. children: mediaAttachments.map((attachment) {
  95. return SizedBox(
  96. width: itemWidth,
  97. child: _buildMediaPreview(context, attachment, mediaAttachments.indexOf(attachment), post.attachments),
  98. );
  99. }).toList(),
  100. ),
  101. ...attachments.map((a) => _buildAttachmentLink(context, a)),
  102. ],
  103. );
  104. },
  105. ),
  106. */
  107. ],
  108. );
  109. }
  110. Widget _buildAttachmentLink(BuildContext context,Attachment attachment) {
  111. return Padding(
  112. padding: const EdgeInsets.only(top: 8),
  113. child: InkWell(
  114. onTap: () => _handleUrlLaunch(context, attachment.link),
  115. child: Row(
  116. children: [
  117. const Icon(Icons.link, size: 16),
  118. const SizedBox(width: 8),
  119. Expanded(
  120. child: Text(
  121. attachment.name,
  122. style: const TextStyle(
  123. color: Colors.blue,
  124. decoration: TextDecoration.underline,
  125. ),
  126. overflow: TextOverflow.ellipsis,
  127. ),
  128. ),
  129. ],
  130. ),
  131. ),
  132. );
  133. }
  134. int _calculateColumnCount(double availableWidth) {
  135. return (availableWidth / 400).floor().clamp(1, 3);
  136. }
  137. Widget _buildMediaPreview(BuildContext context,Attachment attachment, int index, List<Attachment> attachments) {
  138. return GestureDetector(
  139. onTap: () {
  140. _openMediaCarousel(context, index);
  141. },
  142. child: attachment.isVideo
  143. ? VideoPreview(videoUrl: attachment.link, onTap: () => _openMediaCarousel(context, index),)
  144. : (attachment.isImage ? SmartImageContainer(imageUrl: attachment.link) : null),
  145. );
  146. }
  147. void _openMediaCarousel(BuildContext context, int attachmentIndex) {
  148. List<MediaCarouselItem> mediaItems = [];
  149. for (int i = 0; i < allPosts.length; i++) {
  150. final post = allPosts[i];
  151. for (int j = 0; j < post.mediaAttachments.length; j++) {
  152. final attachment = post.mediaAttachments[j];
  153. if (attachment.isImage || attachment.isVideo) {
  154. mediaItems.add(
  155. MediaCarouselItem(
  156. url: attachment.link,
  157. type: attachment.isVideo ? "video" : "image",
  158. postIndex: i,
  159. attachmentIndex: j,
  160. ),
  161. );
  162. }
  163. }
  164. }
  165. int globalIndex = 0;
  166. for (int i = 0; i < postIndex; i++) {
  167. globalIndex += allPosts[i].mediaAttachments.where((x)=>x.isImage || x.isVideo).length;
  168. }
  169. globalIndex += attachmentIndex;
  170. Navigator.push(
  171. context,
  172. MaterialPageRoute(
  173. builder: (context) => MediaCarouselScreen(
  174. initialIndex: globalIndex,
  175. mediaItems: mediaItems,
  176. loadMorePosts: loadMorePosts,
  177. allPosts: allPosts,
  178. ),
  179. ),
  180. );
  181. }
  182. }
  183. class _PostHeader extends StatelessWidget {
  184. final Post post;
  185. const _PostHeader({required this.post});
  186. @override
  187. Widget build(BuildContext context) {
  188. return Padding(
  189. padding: const EdgeInsets.only(bottom: 12),
  190. child: Row(
  191. children: [
  192. /*CircleAvatar(
  193. backgroundImage: NetworkImage(post.author),
  194. radius: 20,
  195. ),*/
  196. const SizedBox(width: 12),
  197. Expanded(
  198. child: Column(
  199. crossAxisAlignment: CrossAxisAlignment.start,
  200. children: [
  201. Text(
  202. post.title,
  203. style: Theme.of(context).textTheme.titleMedium?.copyWith(
  204. fontWeight: FontWeight.w600,
  205. ),
  206. ),
  207. ],
  208. ),
  209. ),
  210. // Time and menu
  211. Text(
  212. post.published,
  213. style: Theme.of(context).textTheme.bodySmall,
  214. ),
  215. /*IconButton(
  216. icon: const Icon(Icons.more_vert),
  217. onPressed: () => _showPostMenu(context),
  218. ),*/
  219. ],
  220. ),
  221. );
  222. }
  223. }
  224. // New footer component
  225. class _PostActionsFooter extends StatelessWidget {
  226. final Post post;
  227. const _PostActionsFooter({required this.post});
  228. @override
  229. Widget build(BuildContext context) {
  230. return Padding(
  231. padding: const EdgeInsets.only(top: 12),
  232. child: Row(
  233. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  234. children: [
  235. // Interactive buttons
  236. Row(
  237. children: [/*
  238. IconButton(
  239. icon: Icon(
  240. post.isLiked ? Icons.favorite : Icons.favorite_border,
  241. color: post.isLiked ? Colors.red : null,
  242. ),
  243. onPressed: () => _toggleLike(),
  244. ),
  245. Text(_formatCount(post.likeCount)),
  246. const SizedBox(width: 16),
  247. IconButton(
  248. icon: const Icon(Icons.comment_outlined),
  249. onPressed: () => _showComments(),
  250. ),
  251. Text(_formatCount(post.commentCount)),
  252. */],
  253. ),
  254. // Share button
  255. IconButton(
  256. icon: const Icon(Icons.share_outlined),
  257. onPressed: () => _sharePost(post),
  258. ),
  259. ],
  260. ),
  261. );
  262. }
  263. }
  264. void _sharePost(Post post) {
  265. var link = '${post.baseUrl}/${post.service}/user/${post.user}/post/${post.id}';
  266. SharePlus.instance.share(ShareParams(uri: Uri.parse(link)));
  267. }
  268. Future<bool> _handleUrlLaunch(BuildContext context, String url) async {
  269. try {
  270. await launchUrl(
  271. Uri.parse(url),
  272. mode: LaunchMode.externalApplication,
  273. );
  274. } catch (e) {
  275. ScaffoldMessenger.of(context).showSnackBar(
  276. SnackBar(content: Text('Could not open link: ${e.toString()}')),
  277. );
  278. }
  279. return true;
  280. }