Flutter 仿掘金推特点赞按钮,kotlin 中文版
likeBuilder: (bool isLiked) {return Icon(Icons.home,color: isLiked ? Colors.deepPurpleAccent : Colors.grey,size: buttonSize,);},likeCount: 665,countBuilder: (int count, bool isLiked, String text) {var color = isLiked ? Colors.deepPurpleAccent : Colors.grey;Widget result;if (count == 0) {result = Text("love",style: TextStyle(color: color),);} elseresult = Text(text,style: TextStyle(color: color),);return result;},),
什么时候去请求服务改变状态
LikeButton(onTap: (bool isLiked){return onLikeButtonTap(isLiked, item);},)
这是一个异步回调,你可以等待服务返回之后再改变状态。也可以先改变状态,请求失败之后重置回状态
Future<bool> onLikeButtonTap(bool isLiked, TuChongItem item) {///send your request here///final Completer<bool> completer = new Completer<bool>();Timer(const Duration(milliseconds: 200), () {item.isFavorite = !item.isFavorite;item.favorites =item.isFavorite ? item.favorites + 1 : item.favorites - 1;
// if your request is failed,return null,completer.complete(item.isFavorite);});return completer.future;}
参数
et 的间隔 | const EdgeInsets.only(left: 3.0) || postion | like count widget 位置(left,right).在 like wiget 的左边或者右边 | Postion.right |
评论