import 'package:comunic/enums/likes_type.dart'; import 'package:comunic/helpers/likes_helper.dart'; 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 { final LikesType likeType; final int likeID; int likesCount; bool isLiking; final UpdatedLikingCallBack onUpdatedLikings; LikeWidget({ Key key, @required this.likeType, @required this.likeID, @required this.likesCount, @required this.isLiking, @required this.onUpdatedLikings, }) : assert(likeType != null), assert(likeID != null), assert(likesCount != null), assert(isLiking != null), assert(onUpdatedLikings != null), super(key: key); @override _LikeWidgetState createState() => _LikeWidgetState(); } class _LikeWidgetState extends SafeState { String get _likeString { switch (widget.likesCount) { case 0: return tr("Like"); case 1: return tr("1 Like"); default: return tr("%num% likes", args: {"num": widget.likesCount.toString()}); } } Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(top: 4.0, bottom: 4.0), child: Align( alignment: AlignmentDirectional.topStart, child: Column( children: [ InkWell( onTap: () => _toggleLike(), child: Row( children: [ Icon( Icons.thumb_up, color: widget.isLiking ? Colors.blue : null, size: 15.0, ), SizedBox( width: 8.0, ), Text(_likeString), ], ), ), ], ), ), ); } /// Toggle like status void _toggleLike() async { // As like are not really important, we ignore failures if (await LikesHelper().setLiking( type: widget.likeType, like: !widget.isLiking, id: widget.likeID)) { setState(() { widget.isLiking = !widget.isLiking; widget.likesCount += widget.isLiking ? 1 : -1; widget.onUpdatedLikings(widget.likesCount, widget.isLiking); }); } } }