From 892fdbebb898fe60995ce424938588975d4af651 Mon Sep 17 00:00:00 2001 From: mso7nueh Date: Wed, 21 Aug 2024 16:07:14 +0300 Subject: [PATCH] blog post --- .../presentation/pages/blog_post_screen.dart | 70 +++++++++++++++++-- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/lib/features/blog_post/presentation/pages/blog_post_screen.dart b/lib/features/blog_post/presentation/pages/blog_post_screen.dart index 183a6558..f1edc633 100644 --- a/lib/features/blog_post/presentation/pages/blog_post_screen.dart +++ b/lib/features/blog_post/presentation/pages/blog_post_screen.dart @@ -4,6 +4,7 @@ import 'package:eatvio_app/features/feed/models/comment_model.dart'; import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart'; import 'package:eatvio_app/features/feed/presentation/bloc/comments_cubit/comments_cubit.dart'; import 'package:eatvio_app/features/feed/presentation/bloc/comments_cubit/comments_state.dart'; +import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_cubit.dart'; import 'package:eatvio_app/locator_service.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -22,10 +23,15 @@ class BlogPostScreen extends StatefulWidget { class _BlogPostScreenState extends State { final CommentsCubit commentsCubit = sl(); final textEditingController = TextEditingController(); + int? replyId; + late bool isLiked; + late int totalLikes; @override void initState() { commentsCubit.getBlogPostComments(widget.blogPost.id); + isLiked = widget.blogPost.wasLikedByMe; + totalLikes = widget.blogPost.totalLikes; super.initState(); } @@ -50,8 +56,32 @@ class _BlogPostScreenState extends State { child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ + if (replyId != null) + GestureDetector( + onTap: () { + setState(() { + replyId = null; + textEditingController.clear(); + }); + FocusManager.instance.primaryFocus?.unfocus(); + }, + child: Container( + decoration: BoxDecoration( + color: appTheme.blueA400, + shape: BoxShape.circle, + ), + height: 32, + width: 32, + child: Icon( + Icons.close_rounded, + size: 18, + color: Colors.white, + ), + ), + ), + if (replyId != null) SizedBox(width: 8), SizedBox( - width: mediaQueryData.size.width - 64, + width: mediaQueryData.size.width - (replyId == null ? 64 : 104), child: TextFormField( onChanged: (_) { setState(() {}); @@ -108,9 +138,11 @@ class _BlogPostScreenState extends State { commentsCubit.sendBlogPostComment( id: widget.blogPost.id, commentText: textEditingController.text, + parentCommentId: replyId, ); setState(() { textEditingController.clear(); + replyId = null; }); FocusManager.instance.primaryFocus?.unfocus(); } @@ -205,7 +237,23 @@ class _BlogPostScreenState extends State { padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), child: Row( children: [ - _iconButton(icon: Icons.favorite_border_rounded, text: widget.blogPost.totalLikes.toString()), + _iconButton( + icon: Icons.favorite_border_rounded, + text: totalLikes.toString(), + iconColor: isLiked ? Colors.red.withOpacity(0.8) : null, + backgroundColor: isLiked ? Colors.red.withOpacity(0.1) : null, + textColor: isLiked ? Colors.red.withOpacity(0.8) : null, + onTap: () async { + setState(() { + isLiked = !isLiked; + isLiked ? totalLikes++ : totalLikes--; + }); + isLiked + ? await context.read().likePost(widget.blogPost.id) + : await context.read().unlikedPost(widget.blogPost.id); + await context.read().loadFeedBlogPostList(); + }, + ), SizedBox(width: 8), _iconButton( icon: Icons.reply_rounded, @@ -293,6 +341,10 @@ class _BlogPostScreenState extends State { _comment(Datum datum, [double radius = 16]) { return Container( width: MediaQuery.of(context).size.width, + decoration: BoxDecoration( + color: replyId == datum.id ? appTheme.blueA400.withOpacity(0.1) : Colors.white, + borderRadius: BorderRadius.circular(16), + ), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Row( @@ -337,7 +389,11 @@ class _BlogPostScreenState extends State { ), SizedBox(width: 16), GestureDetector( - onTap: () {}, + onTap: () { + setState(() { + replyId = datum.id; + }); + }, child: Text( 'Ответить', style: TextStyle( @@ -358,14 +414,14 @@ class _BlogPostScreenState extends State { ); } - _iconButton({required IconData icon, String? text, Function()? onTap}) { + _iconButton({required IconData icon, String? text, Function()? onTap, Color? iconColor, Color? backgroundColor, Color? textColor}) { return InkWell( onTap: onTap ?? () {}, borderRadius: BorderRadius.circular(16), child: Ink( padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4), decoration: BoxDecoration( - color: appTheme.gray100, + color: backgroundColor ?? appTheme.gray100, borderRadius: BorderRadius.circular(16), ), height: 32, @@ -375,14 +431,14 @@ class _BlogPostScreenState extends State { Icon( icon, size: 20, - color: appTheme.gray600, + color: iconColor ?? appTheme.gray600, ), if (text != null) SizedBox(width: 8), if (text != null) Text( text, style: TextStyle( - color: appTheme.gray600, + color: textColor ?? appTheme.gray600, fontWeight: FontWeight.w500, ), ),