PIbd-31_MasenkinMS_PMU/lib/view/home_page/comment.dart

170 lines
4.8 KiB
Dart
Raw Normal View History

2024-09-30 02:09:26 +04:00
part of 'home_page.dart';
typedef OnActionCallback = void Function(
String nickname, bool isLiked, bool isDisliked)?;
class _Comment extends StatefulWidget {
final String title;
final String text;
2024-09-30 22:15:55 +04:00
final UserData? user;
2024-09-30 02:09:26 +04:00
final OnActionCallback onAction;
final VoidCallback? onTap;
const _Comment({
required this.title,
required this.text,
this.user,
this.onAction,
this.onTap,
});
factory _Comment.fromData(
2024-09-30 22:15:55 +04:00
CommentData data, {
2024-09-30 02:09:26 +04:00
OnActionCallback onAction,
VoidCallback? onTap,
}) =>
_Comment(
2024-09-30 22:15:55 +04:00
title: data.title,
text: data.text,
user: data.user,
2024-09-30 02:09:26 +04:00
onAction: onAction,
onTap: onTap,
);
@override
State<_Comment> createState() => _CommentState();
}
class _CommentState extends State<_Comment> {
bool isLiked = false;
bool isDisliked = false;
void _onLike() {
setState(() {
isLiked = !isLiked;
if (isLiked && isDisliked) {
isDisliked = false;
}
});
if (widget.onAction != null) {
widget.onAction?.call(widget.user!.nickname, isLiked, isDisliked);
}
}
void _onDislike() {
setState(() {
isDisliked = !isDisliked;
if (isDisliked && isLiked) {
isLiked = false;
}
});
if (widget.onAction != null) {
widget.onAction?.call(widget.user!.nickname, isLiked, isDisliked);
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
child: Container(
margin: const EdgeInsets.all(16),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: Colors.grey),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(.5),
spreadRadius: 4,
offset: const Offset(0, 5),
blurRadius: 8,
),
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(30),
child: SizedBox(
height: 80,
width: 80,
2024-09-30 18:24:51 +04:00
child: widget.user!.avatarUrl != null &&
widget.user!.avatarUrl!.isNotEmpty
? Image.network(
widget.user!.avatarUrl!,
fit: BoxFit.cover,
)
: Container(
height: 80,
width: 80,
color: Colors.grey,
child: const Icon(Icons.person, size: 40),
),
2024-09-30 02:09:26 +04:00
),
),
const SizedBox(height: 8),
2024-09-30 18:24:51 +04:00
SizedBox(
width: 80,
child: Text(
widget.user!.nickname,
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
2024-09-30 02:09:26 +04:00
),
Row(
children: [
IconButton(
iconSize: 20,
icon: Icon(
isLiked ? Icons.thumb_up : Icons.thumb_up_outlined,
color: isLiked ? Colors.blue : Colors.grey,
),
onPressed: _onLike,
),
IconButton(
iconSize: 20,
icon: Icon(
isDisliked
? Icons.thumb_down
: Icons.thumb_down_outlined,
color: isDisliked ? Colors.red : Colors.grey,
),
onPressed: _onDislike,
),
],
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.title,
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
widget.text,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
),
),
],
),
),
);
}
}