1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-30 14:58:52 +00:00
comunicmobile/lib/ui/widgets/like_widget.dart

93 lines
2.3 KiB
Dart
Raw Normal View History

2020-04-15 16:58:45 +00:00
import 'package:comunic/helpers/likes_helper.dart';
2020-04-15 17:17:29 +00:00
import 'package:comunic/models/like_element.dart';
2020-04-15 16:58:45 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Like widget
///
/// @author Pierre Hubert
/// Updated linking callback
/// int : new number of likes
/// bool : new user liking status
typedef UpdatedLikingCallBack = Function(int, bool);
class LikeWidget extends StatefulWidget {
2020-04-15 17:17:29 +00:00
final LikeElement likeElement;
2020-04-15 17:26:51 +00:00
final double buttonIconSize;
2021-03-16 17:27:02 +00:00
final Color activeColor;
final Color inativeColor;
2020-04-15 16:58:45 +00:00
2021-03-16 17:27:02 +00:00
const LikeWidget(
{Key key,
@required this.likeElement,
this.buttonIconSize = 15.0,
this.activeColor,
this.inativeColor})
: assert(likeElement != null),
2020-04-15 16:58:45 +00:00
super(key: key);
@override
_LikeWidgetState createState() => _LikeWidgetState();
}
class _LikeWidgetState extends SafeState<LikeWidget> {
2020-04-15 17:17:29 +00:00
LikeElement get elem => widget.likeElement;
2020-04-15 16:58:45 +00:00
String get _likeString {
2020-04-15 17:17:29 +00:00
switch (elem.likes) {
2020-04-15 16:58:45 +00:00
case 0:
return tr("Like");
case 1:
return tr("1 Like");
default:
2020-04-15 17:17:29 +00:00
return tr("%num% likes", args: {"num": elem.likes.toString()});
2020-04-15 16:58:45 +00:00
}
}
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 4.0, bottom: 4.0),
2020-04-15 17:07:15 +00:00
child: IntrinsicWidth(
child: InkWell(
onTap: () => _toggleLike(),
child: Row(
children: <Widget>[
Icon(
Icons.thumb_up,
2021-03-16 17:27:02 +00:00
color: elem.userLike
? (widget.activeColor ?? Colors.blue)
: widget.inativeColor,
2020-04-15 17:26:51 +00:00
size: widget.buttonIconSize,
2020-04-15 16:58:45 +00:00
),
2020-04-15 17:07:15 +00:00
SizedBox(
width: 8.0,
),
Text(_likeString),
],
),
2020-04-15 16:58:45 +00:00
),
),
);
}
/// Toggle like status
void _toggleLike() async {
// As like are not really important, we ignore failures
2020-04-18 13:24:57 +00:00
try {
await LikesHelper()
.setLiking(type: elem.likeType, like: !elem.userLike, id: elem.id);
2020-04-15 16:58:45 +00:00
setState(() {
2020-04-15 17:17:29 +00:00
elem.userLike = !elem.userLike;
2020-04-15 16:58:45 +00:00
2020-04-15 17:17:29 +00:00
elem.likes += elem.userLike ? 1 : -1;
2020-04-15 16:58:45 +00:00
});
2020-04-18 13:24:57 +00:00
} catch (e, stack) {
print("$e\n$stack");
2020-04-15 16:58:45 +00:00
}
}
}