From 7f1130aca82a436b1422986a72e6dc6f56b17a8c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 15 May 2020 13:34:53 +0200 Subject: [PATCH] Do not show too many comments at the same time --- lib/ui/tiles/post_tile.dart | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 0fae3f5..c5ba403 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'dart:math'; import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/enums/post_visibility_level.dart'; @@ -76,6 +77,7 @@ class _PostTileState extends State { TextEditingController _commentController = TextEditingController(); File _commentImage; bool _submitting = false; + int _maxNumberOfCommentToShow = 10; User get _user => widget.usersInfo.getUser(widget.post.userID); @@ -344,13 +346,18 @@ class _PostTileState extends State { assert(widget.post.hasComments); final comments = List.generate( - widget.post.comments.length, - (num) => CommentTile( - comment: widget.post.comments[num], - user: widget.usersInfo.getUser(widget.post.comments[num].userID), - onUpdateComment: _updateCommentContent, - onDeleteComment: _deleteComment, - ), + min(widget.post.comments.length, _maxNumberOfCommentToShow), + (num) { + final index = num + + max(0, widget.post.comments.length - _maxNumberOfCommentToShow); + final comment = widget.post.comments[index]; + return CommentTile( + comment: comment, + user: widget.usersInfo.getUser(comment.userID), + onUpdateComment: _updateCommentContent, + onDeleteComment: _deleteComment, + ); + }, ); // Add comments form @@ -361,12 +368,23 @@ class _PostTileState extends State { child: Padding( padding: const EdgeInsets.only(top: 8.0, bottom: 8.0), child: Column( - children: comments, + children: (widget.post.comments.length > _maxNumberOfCommentToShow + ? [_buildShowMoreCommentsButton()] + : []) + ..addAll(comments), ), ), ); } + Widget _buildShowMoreCommentsButton() => InkWell( + onTap: () => setState(() => _maxNumberOfCommentToShow += 10), + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Text(tr("Show more comments")), + ), + ); + /// Build comments form Widget _buildCommentsForm() { return Padding(