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

147 lines
4.4 KiB
Dart
Raw Normal View History

2020-08-31 18:26:57 +02:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
2020-08-31 20:10:05 +02:00
import '../comment_tree.dart';
import 'markdown_text.dart';
class Comment extends StatelessWidget {
final int indent;
2020-08-31 15:29:02 +02:00
final int postCreatorId;
final CommentTree commentTree;
Comment(
this.commentTree, {
this.indent = 0,
2020-08-31 15:29:02 +02:00
@required this.postCreatorId,
});
2020-08-31 15:29:02 +02:00
void _goToUser() {
print('GO TO USER');
}
bool get isOP => commentTree.comment.creatorId == postCreatorId;
@override
Widget build(BuildContext context) {
var comment = commentTree.comment;
2020-08-31 13:39:27 +02:00
// decide which username to use
2020-09-01 14:17:56 +02:00
final username = () {
if (comment.creatorPreferredUsername != null &&
comment.creatorPreferredUsername != '') {
return comment.creatorPreferredUsername;
} else {
return '@${comment.creatorName}';
}
}();
final body = () {
if (comment.deleted) {
return Flexible(
child: Text(
'comment deleted by creator',
style: TextStyle(fontStyle: FontStyle.italic),
));
} else if (comment.removed) {
return Flexible(
child: Text(
'comment deleted by moderator',
style: TextStyle(fontStyle: FontStyle.italic),
));
} else {
return Flexible(child: MarkdownText(commentTree.comment.content));
}
}();
return Column(
children: [
Container(
child: Column(
children: [
Row(children: [
2020-08-31 15:29:02 +02:00
if (comment.creatorAvatar != null)
InkWell(
onTap: _goToUser,
child: Padding(
padding: const EdgeInsets.only(right: 5),
child: CachedNetworkImage(
imageUrl: comment.creatorAvatar,
height: 20,
width: 20,
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: imageProvider,
),
),
),
),
),
),
InkWell(
child: Text(username,
style: TextStyle(
color: Theme.of(context).accentColor,
)),
onLongPress: _goToUser,
),
if (isOP) CommentTag('OP', Theme.of(context).accentColor),
if (comment.banned) CommentTag('BANNED', Colors.red),
if (comment.bannedFromCommunity)
CommentTag('BANNED FROM COMMUNITY', Colors.red),
Spacer(),
Text(comment.score.toString()),
]),
2020-08-31 12:23:32 +02:00
Row(children: [body]),
Row(children: [
Spacer(),
// actions go here
])
],
),
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(left: indent > 1 ? (indent - 1) * 5.0 : 0),
decoration: BoxDecoration(
border: Border(
left: indent > 0
? BorderSide(color: Colors.red, width: 5)
: BorderSide.none,
top: BorderSide(width: 0.2))),
),
for (var c in commentTree.children)
Comment(
2020-08-31 15:29:02 +02:00
c,
indent: indent + 1,
postCreatorId: postCreatorId,
),
],
);
}
}
2020-08-31 15:29:02 +02:00
class CommentTag extends StatelessWidget {
final String text;
final Color bgColor;
const CommentTag(this.text, this.bgColor);
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.only(left: 5),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5)),
color: bgColor,
),
padding: EdgeInsets.symmetric(horizontal: 3, vertical: 2),
child: Text(text,
style: TextStyle(
color: Colors.white,
fontSize: Theme.of(context).textTheme.bodyText1.fontSize - 5,
fontWeight: FontWeight.w800,
)),
),
);
}