lemmur-app-android/lib/widgets/comment_section.dart

92 lines
3.0 KiB
Dart
Raw Normal View History

2020-08-31 21:37:24 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../comment_tree.dart';
import 'comment.dart';
/// Manages comments section, sorts them
class CommentSection extends HookWidget {
final List<CommentView> rawComments;
final List<CommentTree> comments;
final int postCreatorId;
final CommentSortType sortType;
2020-08-31 21:37:24 +02:00
CommentSection(
List<CommentView> rawComments, {
@required this.postCreatorId,
this.sortType = CommentSortType.hot,
}) : comments =
CommentTree.sortList(sortType, CommentTree.fromList(rawComments)),
rawComments = rawComments
..sort((b, a) => a.published.compareTo(b.published)),
2020-08-31 21:37:24 +02:00
assert(postCreatorId != null);
@override
Widget build(BuildContext context) {
var sorting = useState(sortType);
var rawComms = useState(rawComments);
var comms = useState(comments);
void sortComments(CommentSortType sort) {
if (sort == sorting.value || sort == CommentSortType.chat) return;
CommentTree.sortList(sort, comms.value);
}
return Column(children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Row(
children: [
Container(
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
decoration: BoxDecoration(
border: Border.all(color: Colors.black45),
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: DropdownButton(
// TODO: change it to universal BottomModal
underline: Container(),
isDense: true,
onChanged: sortComments,
value: sorting.value,
items: [
DropdownMenuItem(
child: Text('Hot'), value: CommentSortType.hot),
DropdownMenuItem(
child: Text('Top'), value: CommentSortType.top),
DropdownMenuItem(
child: Text('New'), value: CommentSortType.new_),
DropdownMenuItem(
child: Text('Old'), value: CommentSortType.old),
DropdownMenuItem(
child: Text('Chat'), value: CommentSortType.chat),
],
),
2020-08-31 21:37:24 +02:00
),
Spacer(),
],
),
),
// sorting menu goes here
if (comments.isEmpty)
Padding(
padding: EdgeInsets.symmetric(vertical: 50),
child: Text(
'no comments yet',
style: TextStyle(fontStyle: FontStyle.italic),
2020-08-31 21:37:24 +02:00
),
)
else if (sorting.value == CommentSortType.chat)
for (final com in rawComms.value)
Comment(
CommentTree(com),
postCreatorId: postCreatorId,
)
else
for (var com in comms.value) Comment(com, postCreatorId: postCreatorId),
]);
}
2020-08-31 21:37:24 +02:00
}