post_container.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 'smart_image_container.dart';
  6. import '../../models/post.dart';
  7. class PostContainer extends StatelessWidget {
  8. final Post post;
  9. const PostContainer({super.key, required this.post});
  10. @override
  11. Widget build(BuildContext context) {
  12. return Card(
  13. margin: const EdgeInsets.all(8),
  14. child: Padding(
  15. padding: const EdgeInsets.all(16),
  16. child: Column(
  17. mainAxisSize: MainAxisSize.min,
  18. crossAxisAlignment: CrossAxisAlignment.start,
  19. children: [
  20. // Post Header
  21. _PostHeader(post: post),
  22. // Auto-sizing Content
  23. _PostContentSection(post: post),
  24. // Interactive Footer
  25. _PostActionsFooter(post: post),
  26. ],
  27. ),
  28. ),
  29. );
  30. }
  31. }
  32. // New header component
  33. class _PostHeader extends StatelessWidget {
  34. final Post post;
  35. const _PostHeader({required this.post});
  36. @override
  37. Widget build(BuildContext context) {
  38. return Padding(
  39. padding: const EdgeInsets.only(bottom: 12),
  40. child: Row(
  41. children: [
  42. // Author avatar
  43. /*CircleAvatar(
  44. backgroundImage: NetworkImage(post.author),
  45. radius: 20,
  46. ),*/
  47. const SizedBox(width: 12),
  48. // Author info
  49. Expanded(
  50. child: Column(
  51. crossAxisAlignment: CrossAxisAlignment.start,
  52. children: [
  53. Text(
  54. post.title,
  55. style: Theme.of(context).textTheme.titleMedium?.copyWith(
  56. fontWeight: FontWeight.w600,
  57. ),
  58. ),
  59. Text(
  60. post.published,
  61. style: Theme.of(context).textTheme.bodySmall,
  62. ),
  63. ],
  64. ),
  65. ),
  66. // Time and menu
  67. Text(
  68. post.published,
  69. style: Theme.of(context).textTheme.bodySmall,
  70. ),
  71. /*IconButton(
  72. icon: const Icon(Icons.more_vert),
  73. onPressed: () => _showPostMenu(context),
  74. ),*/
  75. ],
  76. ),
  77. );
  78. }
  79. }
  80. // New footer component
  81. class _PostActionsFooter extends StatelessWidget {
  82. final Post post;
  83. const _PostActionsFooter({required this.post});
  84. @override
  85. Widget build(BuildContext context) {
  86. return Padding(
  87. padding: const EdgeInsets.only(top: 12),
  88. child: Row(
  89. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  90. children: [
  91. // Interactive buttons
  92. Row(
  93. children: [/*
  94. IconButton(
  95. icon: Icon(
  96. post.isLiked ? Icons.favorite : Icons.favorite_border,
  97. color: post.isLiked ? Colors.red : null,
  98. ),
  99. onPressed: () => _toggleLike(),
  100. ),
  101. Text(_formatCount(post.likeCount)),
  102. const SizedBox(width: 16),
  103. IconButton(
  104. icon: const Icon(Icons.comment_outlined),
  105. onPressed: () => _showComments(),
  106. ),
  107. Text(_formatCount(post.commentCount)),
  108. */],
  109. ),
  110. // Share button
  111. IconButton(
  112. icon: const Icon(Icons.share_outlined),
  113. onPressed: () => _sharePost(post),
  114. ),
  115. ],
  116. ),
  117. );
  118. }
  119. }
  120. void _sharePost(Post post) {
  121. //create link to share
  122. }
  123. // Modified content section with clickable links
  124. class _PostContentSection extends StatelessWidget {
  125. final Post post;
  126. const _PostContentSection({required this.post});
  127. @override
  128. Widget build(BuildContext context) {
  129. return Column(
  130. mainAxisSize: MainAxisSize.min,
  131. crossAxisAlignment: CrossAxisAlignment.start,
  132. children: [
  133. // Text Content
  134. HtmlWidget(
  135. post.content,
  136. textStyle: Theme.of(context).textTheme.bodyMedium,
  137. onTapUrl: (url) => _handleUrlLaunch(context, url),
  138. ),
  139. // Attachments Grid
  140. if (post.attachments.isNotEmpty)
  141. LayoutBuilder(
  142. builder: (context, constraints) {
  143. return Column(
  144. children: [
  145. const SizedBox(height: 12),
  146. GridView.builder(
  147. shrinkWrap: true,
  148. physics: const NeverScrollableScrollPhysics(),
  149. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  150. crossAxisCount: _calculateColumnCount(constraints.maxWidth),
  151. ),
  152. itemCount: post.attachments.length,
  153. itemBuilder: (context, index) => Padding(
  154. padding: const EdgeInsets.all(4),
  155. child: SmartImageContainer(
  156. imageUrl: post.attachments[index].link,
  157. //onTap: () => _handleAttachmentTap(index),
  158. ),
  159. ),
  160. ),
  161. // Attachment links list
  162. ...post.attachments.map(
  163. (attachment) => _buildAttachmentLink(context,attachment),
  164. ),
  165. ],
  166. );
  167. },
  168. ),
  169. ],
  170. );
  171. }
  172. Widget _buildAttachmentLink(BuildContext context,Attachment attachment) {
  173. return Padding(
  174. padding: const EdgeInsets.only(top: 8),
  175. child: InkWell(
  176. onTap: () => _handleUrlLaunch(context, attachment.link),
  177. child: Row(
  178. children: [
  179. const Icon(Icons.link, size: 16),
  180. const SizedBox(width: 8),
  181. Expanded(
  182. child: Text(
  183. attachment.name ?? _parseFileName(attachment.link),
  184. style: const TextStyle(
  185. color: Colors.blue,
  186. decoration: TextDecoration.underline,
  187. ),
  188. overflow: TextOverflow.ellipsis,
  189. ),
  190. ),
  191. ],
  192. ),
  193. ),
  194. );
  195. }
  196. int _calculateColumnCount(double availableWidth) {
  197. return (availableWidth / 400).floor().clamp(1, 3);
  198. }
  199. String _parseFileName(String url) {
  200. try {
  201. return Uri.parse(url).pathSegments.last;
  202. } catch (e) {
  203. return 'Download';
  204. }
  205. }
  206. }
  207. // URL handling method (add to parent class)
  208. Future<bool> _handleUrlLaunch(BuildContext context, String url) async {
  209. try {
  210. if (await canLaunchUrl(Uri.parse(url))) {
  211. await launchUrl(
  212. Uri.parse(url),
  213. mode: LaunchMode.externalApplication,
  214. );
  215. }
  216. } catch (e) {
  217. ScaffoldMessenger.of(context).showSnackBar(
  218. SnackBar(content: Text('Could not open link: ${e.toString()}')),
  219. );
  220. }
  221. return true;
  222. }