From 2d26395d73c4dae840bff9c3af426d63287210c0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 15 Apr 2020 19:07:15 +0200 Subject: [PATCH] Use Like widget on Post element --- lib/ui/tiles/post_tile.dart | 55 ++++++--------------------------- lib/ui/widgets/like_widget.dart | 35 +++++++++------------ 2 files changed, 25 insertions(+), 65 deletions(-) diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 1b114e3..67841c1 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -9,13 +9,13 @@ import 'package:comunic/helpers/posts_helper.dart'; import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/comment.dart'; -import 'package:comunic/models/like_element.dart'; import 'package:comunic/models/new_comment.dart'; import 'package:comunic/models/post.dart'; import 'package:comunic/models/user.dart'; import 'package:comunic/ui/tiles/comment_tile.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/countdown_widget.dart'; +import 'package:comunic/ui/widgets/like_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; import 'package:comunic/ui/widgets/survey_widget.dart'; import 'package:comunic/utils/date_utils.dart'; @@ -71,7 +71,6 @@ class PostTile extends StatefulWidget { class _PostTileState extends State { // Helpers final _postsHelper = PostsHelper(); - final _likesHelper = LikesHelper(); final _commentsHelper = CommentsHelper(); // Class members @@ -212,28 +211,15 @@ class _PostTileState extends State { children: [ // Like button Center( - child: InkWell( - onTap: () => _updatePostLike(), - child: Row( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: const EdgeInsets.only(left: 8.0, right: 8.0), - child: Icon( - Icons.thumb_up, - color: widget.post.userLike ? Colors.blue : null, - ), - ), - Text(widget.post.likes < 2 - ? tr("%num% like", - args: {"num": widget.post.likes.toString()}) - : tr("%num% likes", - args: {"num": widget.post.likes.toString()})) - ], - ), - ), - ), + child: LikeWidget( + likeType: LikesType.POST, + likeID: widget.post.id, + likesCount: widget.post.likes, + isLiking: widget.post.userLike, + onUpdatedLikings: (num, userLike) { + widget.post.likes = num; + widget.post.userLike = userLike; + })), ], ), ); @@ -519,27 +505,6 @@ class _PostTileState extends State { }); } - /// Update like status - Future _updatePostLike() async { - _updateElementLike(LikesType.POST, widget.post); - } - - /// Invert element like status - Future _updateElementLike(LikesType type, LikeElement element) async { - // Update liking status - _likesHelper.setLiking( - type: type, - like: !element.userLike, - id: element.id, - ); - - // Save new like status - setState(() { - element.userLike = !element.userLike; - element.userLike ? element.likes++ : element.likes--; - }); - } - /// Update comment content Future _updateCommentContent(Comment comment) async { final newContent = await askUserString( diff --git a/lib/ui/widgets/like_widget.dart b/lib/ui/widgets/like_widget.dart index 59b875c..a634100 100644 --- a/lib/ui/widgets/like_widget.dart +++ b/lib/ui/widgets/like_widget.dart @@ -55,27 +55,22 @@ class _LikeWidgetState extends SafeState { Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(top: 4.0, bottom: 4.0), - child: Align( - alignment: AlignmentDirectional.topStart, - child: Column( - children: [ - InkWell( - onTap: () => _toggleLike(), - child: Row( - children: [ - Icon( - Icons.thumb_up, - color: widget.isLiking ? Colors.blue : null, - size: 15.0, - ), - SizedBox( - width: 8.0, - ), - Text(_likeString), - ], + child: IntrinsicWidth( + child: InkWell( + onTap: () => _toggleLike(), + child: Row( + children: [ + Icon( + Icons.thumb_up, + color: widget.isLiking ? Colors.blue : null, + size: 15.0, ), - ), - ], + SizedBox( + width: 8.0, + ), + Text(_likeString), + ], + ), ), ), );