pikapika/lib/screens/components/CommentItem.dart

184 lines
7.7 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/basic/Method.dart';
2021-09-29 23:57:09 +00:00
2021-11-09 02:38:04 +00:00
import 'Avatar.dart';
2021-11-09 21:57:44 +00:00
import 'CommentMainType.dart';
2021-09-29 23:57:09 +00:00
2021-11-02 05:43:22 +00:00
class ComicCommentItem extends StatefulWidget {
2021-11-09 21:57:44 +00:00
final CommentMainType mainType;
final String mainId;
final CommentBase comment;
2021-09-29 23:57:09 +00:00
2021-11-09 21:57:44 +00:00
const ComicCommentItem(this.mainType, this.mainId, this.comment);
2021-11-02 05:43:22 +00:00
@override
State<StatefulWidget> createState() => _ComicCommentItem();
}
class _ComicCommentItem extends State<ComicCommentItem> {
var likeLoading = false;
2021-09-29 23:57:09 +00:00
@override
Widget build(BuildContext context) {
2021-11-02 05:43:22 +00:00
var comment = widget.comment;
2021-09-29 23:57:09 +00:00
var theme = Theme.of(context);
var nameStyle = TextStyle(fontWeight: FontWeight.bold);
var levelStyle = TextStyle(
fontSize: 12, color: theme.colorScheme.secondary.withOpacity(.8));
var connectStyle =
TextStyle(color: theme.textTheme.bodyText1?.color?.withOpacity(.8));
var datetimeStyle = TextStyle(
color: theme.textTheme.bodyText1?.color?.withOpacity(.6), fontSize: 12);
return Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: .25,
style: BorderStyle.solid,
color: Colors.grey.shade500.withOpacity(.5),
),
bottom: BorderSide(
width: .25,
style: BorderStyle.solid,
color: Colors.grey.shade500.withOpacity(.5),
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-11-09 02:38:04 +00:00
Avatar(comment.user.avatar),
2021-09-29 23:57:09 +00:00
Container(width: 5),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
width: constraints.maxWidth,
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
alignment: WrapAlignment.spaceBetween,
children: [
Text(comment.user.name, style: nameStyle),
Text(
formatTimeToDateTime(comment.createdAt),
style: datetimeStyle,
),
],
),
);
},
),
Container(height: 3),
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
width: constraints.maxWidth,
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
alignment: WrapAlignment.spaceBetween,
children: [
Text(
"Lv. ${comment.user.level} (${comment.user.title})",
style: levelStyle),
2021-11-02 05:07:28 +00:00
Text.rich(TextSpan(
style: levelStyle,
children: [
comment.commentsCount > 0
? TextSpan(children: [
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Icon(Icons.message,
size: 13,
color: theme.colorScheme.secondary
.withOpacity(.7)),
),
WidgetSpan(child: Container(width: 5)),
TextSpan(
text: '${comment.commentsCount}',
),
])
: TextSpan(),
WidgetSpan(child: Container(width: 12)),
2021-11-02 05:43:22 +00:00
WidgetSpan(
child: GestureDetector(
onTap: () async {
setState(() {
likeLoading = true;
});
try {
2021-11-09 21:57:44 +00:00
switch (widget.mainType) {
case CommentMainType.COMIC:
await method.switchLikeComment(
comment.id,
widget.mainId,
);
break;
case CommentMainType.GAME:
await method.switchLikeGameComment(
comment.id,
widget.mainId,
);
break;
}
2021-11-02 05:43:22 +00:00
setState(() {
if (comment.isLiked) {
comment.isLiked = false;
comment.likesCount--;
} else {
comment.isLiked = true;
comment.likesCount++;
}
});
} catch (e, s) {
defaultToast(context, "点赞失败");
} finally {
setState(() {
likeLoading = false;
});
}
},
child: Text.rich(
TextSpan(style: levelStyle, children: [
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Icon(
likeLoading
? Icons.refresh
: comment.isLiked
? Icons.favorite
: Icons.favorite_border,
size: 13,
color: theme.colorScheme.secondary
.withOpacity(.7)),
),
WidgetSpan(child: Container(width: 5)),
TextSpan(
text: '${comment.likesCount}',
),
]),
2021-11-02 05:07:28 +00:00
),
2021-11-02 05:43:22 +00:00
)),
2021-11-02 05:07:28 +00:00
],
)),
2021-09-29 23:57:09 +00:00
],
),
);
},
),
Container(height: 5),
Text(comment.content, style: connectStyle),
],
),
),
],
),
);
}
}