1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-23 05:19:22 +00:00
comunicmobile/lib/ui/widgets/like_widget.dart

84 lines
2.0 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;
2020-04-15 16:58:45 +00:00
2020-04-15 17:17:29 +00:00
const LikeWidget({
2020-04-15 16:58:45 +00:00
Key key,
2020-04-15 17:17:29 +00:00
@required this.likeElement,
2020-04-15 17:26:51 +00:00
this.buttonIconSize = 15.0,
2020-04-15 17:17:29 +00:00
}) : 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,
2020-04-15 17:17:29 +00:00
color: elem.userLike ? Colors.blue : null,
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-15 17:17:29 +00:00
if (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
});
}
}
}