post_container.dart 6.3 KB

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