104 lines
2.9 KiB
Dart
104 lines
2.9 KiB
Dart
import 'package:didvan/constants/app_icons.dart';
|
|
import 'package:didvan/models/view/action_sheet_data.dart';
|
|
import 'package:didvan/providers/user.dart';
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
import 'package:didvan/views/widgets/didvan/text.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class LikedButton extends StatefulWidget {
|
|
final bool value;
|
|
final Color? color;
|
|
final void Function(bool value) onMarkChanged;
|
|
final bool askForConfirmation;
|
|
final double gestureSize;
|
|
final String type;
|
|
final int itemId;
|
|
final int likes;
|
|
const LikedButton({
|
|
Key? key,
|
|
required this.value,
|
|
required this.onMarkChanged,
|
|
required this.gestureSize,
|
|
required this.type,
|
|
required this.itemId,
|
|
required this.likes,
|
|
this.askForConfirmation = false,
|
|
this.color,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<LikedButton> createState() => _LikedButtonState();
|
|
}
|
|
|
|
class _LikedButtonState extends State<LikedButton> {
|
|
bool _value = false;
|
|
|
|
@override
|
|
void didUpdateWidget(covariant LikedButton oldWidget) {
|
|
_value = widget.value;
|
|
super.didUpdateWidget(oldWidget);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
_value = widget.value;
|
|
super.initState();
|
|
}
|
|
|
|
late int likes = widget.likes;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
if (likes != 0)
|
|
SizedBox(
|
|
height: 16,
|
|
child: DidvanText(
|
|
likes.toString(),
|
|
fontSize: 14,
|
|
),
|
|
),
|
|
InkWell(
|
|
onTap: () async {
|
|
bool confirm = false;
|
|
if (widget.askForConfirmation) {
|
|
await ActionSheetUtils(context).openDialog(
|
|
data: ActionSheetData(
|
|
content: const DidvanText(
|
|
'آیا میخواهید این محتوا از نشان شدهها حذف شود؟',
|
|
),
|
|
titleIcon: DidvanIcons.bookmark_regular,
|
|
titleColor: Theme.of(context).colorScheme.secondary,
|
|
title: 'تایید عملیات',
|
|
onConfirmed: () => confirm = true,
|
|
),
|
|
);
|
|
}
|
|
if (!widget.askForConfirmation || confirm) {
|
|
setState(() {
|
|
_value = !_value;
|
|
if (_value) {
|
|
likes += 1;
|
|
} else {
|
|
likes -= 1;
|
|
}
|
|
});
|
|
widget.onMarkChanged(_value);
|
|
UserProvider.changeItemLiked(widget.type, widget.itemId, _value);
|
|
}
|
|
},
|
|
child: Icon(
|
|
_value ? CupertinoIcons.heart_fill : CupertinoIcons.heart,
|
|
size: 24,
|
|
color: widget.color ??
|
|
(!_value ? null : Theme.of(context).colorScheme.error),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|