post_container.dart 6.6 KB

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