post_container.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.attachments),
  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, attachments)
  70. /*SmartImageContainer(
  71. imageUrl: imageAttachments[index].link,
  72. //onTap: () => _handleAttachmentTap(index),
  73. ),*/
  74. ),
  75. ),
  76. // Attachment links list
  77. ...post.attachments.map(
  78. (attachment) => _buildAttachmentLink(context,attachment),
  79. ),
  80. ],
  81. );
  82. },
  83. ),
  84. ],
  85. );
  86. }
  87. Widget _buildAttachmentLink(BuildContext context,Attachment attachment) {
  88. return Padding(
  89. padding: const EdgeInsets.only(top: 8),
  90. child: InkWell(
  91. onTap: () => _handleUrlLaunch(context, attachment.link),
  92. child: Row(
  93. children: [
  94. const Icon(Icons.link, size: 16),
  95. const SizedBox(width: 8),
  96. Expanded(
  97. child: Text(
  98. attachment.name,
  99. style: const TextStyle(
  100. color: Colors.blue,
  101. decoration: TextDecoration.underline,
  102. ),
  103. overflow: TextOverflow.ellipsis,
  104. ),
  105. ),
  106. ],
  107. ),
  108. ),
  109. );
  110. }
  111. int _calculateColumnCount(double availableWidth) {
  112. return (availableWidth / 400).floor().clamp(1, 3);
  113. }
  114. Widget _buildMediaPreview(BuildContext context,Attachment attachment, int index, List<Attachment> attachments) {
  115. return GestureDetector(
  116. onTap: () {
  117. _openMediaCarousel(context, index);
  118. },
  119. child: attachment.isVideo
  120. ? VideoPreview(videoUrl: attachment.link, onTap: () => _openMediaCarousel(context, index),)
  121. : (attachment.isImage ? SmartImageContainer(imageUrl: attachment.link) : null),
  122. );
  123. }
  124. void _openMediaCarousel(BuildContext context, int attachmentIndex) {
  125. List<MediaCarouselItem> mediaItems = [];
  126. for (int i = 0; i < allPosts.length; i++) {
  127. final post = allPosts[i];
  128. for (int j = 0; j < post.attachments.length; j++) {
  129. final attachment = post.attachments[j];
  130. if (attachment.isImage || attachment.isVideo) {
  131. mediaItems.add(
  132. MediaCarouselItem(
  133. url: attachment.link,
  134. type: attachment.isVideo ? "video" : "image",
  135. postIndex: i,
  136. attachmentIndex: j,
  137. ),
  138. );
  139. }
  140. }
  141. }
  142. int globalIndex = 0;
  143. for (int i = 0; i < postIndex; i++) {
  144. globalIndex += allPosts[i].attachments.where((x)=>x.isImage || x.isVideo).length;
  145. }
  146. globalIndex += attachmentIndex;
  147. Navigator.push(
  148. context,
  149. MaterialPageRoute(
  150. builder: (context) => MediaCarouselScreen(
  151. initialIndex: globalIndex,
  152. mediaItems: mediaItems,
  153. loadMorePosts: loadMorePosts,
  154. allPosts: allPosts,
  155. ),
  156. ),
  157. );
  158. }
  159. }
  160. class _PostHeader extends StatelessWidget {
  161. final Post post;
  162. const _PostHeader({required this.post});
  163. @override
  164. Widget build(BuildContext context) {
  165. return Padding(
  166. padding: const EdgeInsets.only(bottom: 12),
  167. child: Row(
  168. children: [
  169. /*CircleAvatar(
  170. backgroundImage: NetworkImage(post.author),
  171. radius: 20,
  172. ),*/
  173. const SizedBox(width: 12),
  174. Expanded(
  175. child: Column(
  176. crossAxisAlignment: CrossAxisAlignment.start,
  177. children: [
  178. Text(
  179. post.title,
  180. style: Theme.of(context).textTheme.titleMedium?.copyWith(
  181. fontWeight: FontWeight.w600,
  182. ),
  183. ),
  184. ],
  185. ),
  186. ),
  187. // Time and menu
  188. Text(
  189. post.published,
  190. style: Theme.of(context).textTheme.bodySmall,
  191. ),
  192. /*IconButton(
  193. icon: const Icon(Icons.more_vert),
  194. onPressed: () => _showPostMenu(context),
  195. ),*/
  196. ],
  197. ),
  198. );
  199. }
  200. }
  201. // New footer component
  202. class _PostActionsFooter extends StatelessWidget {
  203. final Post post;
  204. const _PostActionsFooter({required this.post});
  205. @override
  206. Widget build(BuildContext context) {
  207. return Padding(
  208. padding: const EdgeInsets.only(top: 12),
  209. child: Row(
  210. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  211. children: [
  212. // Interactive buttons
  213. Row(
  214. children: [/*
  215. IconButton(
  216. icon: Icon(
  217. post.isLiked ? Icons.favorite : Icons.favorite_border,
  218. color: post.isLiked ? Colors.red : null,
  219. ),
  220. onPressed: () => _toggleLike(),
  221. ),
  222. Text(_formatCount(post.likeCount)),
  223. const SizedBox(width: 16),
  224. IconButton(
  225. icon: const Icon(Icons.comment_outlined),
  226. onPressed: () => _showComments(),
  227. ),
  228. Text(_formatCount(post.commentCount)),
  229. */],
  230. ),
  231. // Share button
  232. IconButton(
  233. icon: const Icon(Icons.share_outlined),
  234. onPressed: () => _sharePost(post),
  235. ),
  236. ],
  237. ),
  238. );
  239. }
  240. }
  241. void _sharePost(Post post) {
  242. var link = '${post.baseUrl}/${post.service}/user/${post.user}/post/${post.id}';
  243. SharePlus.instance.share(ShareParams(uri: Uri.parse(link)));
  244. }
  245. Future<bool> _handleUrlLaunch(BuildContext context, String url) async {
  246. try {
  247. await launchUrl(
  248. Uri.parse(url),
  249. mode: LaunchMode.externalApplication,
  250. );
  251. } catch (e) {
  252. ScaffoldMessenger.of(context).showSnackBar(
  253. SnackBar(content: Text('Could not open link: ${e.toString()}')),
  254. );
  255. }
  256. return true;
  257. }