194 lines
5.7 KiB
Dart
194 lines
5.7 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:hgmj/assets_tool/images.dart';
|
|
import 'package:hgmj/hj_utils/screen.dart';
|
|
|
|
import '../../assets_tool/app_colors.dart';
|
|
import '../../hj_utils/api_service/acg_service.dart';
|
|
import '../../hj_utils/api_service/mine_service.dart';
|
|
import 'package:hgmj/tools_base/toast.dart';
|
|
import '../../hj_utils/widget_util.dart';
|
|
|
|
enum FollowEnum {
|
|
cartoon, //动画,加入书架
|
|
user, // 用户
|
|
tag, // 帖子标签
|
|
noval, //小说收藏
|
|
}
|
|
|
|
class FollowButton extends StatefulWidget {
|
|
final String? mediaId;
|
|
final FollowEnum followType; //ui样式
|
|
final bool? isFollow;
|
|
|
|
/// 成功回调,回传操作后的最新状态
|
|
final Function(bool isFollow)? onSuccess;
|
|
|
|
const FollowButton({
|
|
super.key,
|
|
this.mediaId,
|
|
required this.followType,
|
|
this.isFollow,
|
|
this.onSuccess,
|
|
});
|
|
|
|
@override
|
|
State<FollowButton> createState() => _FollowButtonState();
|
|
}
|
|
|
|
class _FollowButtonState extends State<FollowButton> {
|
|
String? get mediaId => widget.mediaId;
|
|
FollowEnum get followType => widget.followType;
|
|
bool get isFollow => widget.isFollow ?? false;
|
|
|
|
bool isLoading = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
switch (followType) {
|
|
case FollowEnum.cartoon:
|
|
return _collectView();
|
|
case FollowEnum.user:
|
|
case FollowEnum.tag:
|
|
return _followView();
|
|
case FollowEnum.noval:
|
|
return _novalView();
|
|
}
|
|
}
|
|
|
|
//收藏/加入书架样式
|
|
Widget _collectView() {
|
|
return InkWell(
|
|
enableFeedback: false,
|
|
onTap: _onCollectTap,
|
|
child: isLoading
|
|
? _loading()
|
|
: Image.asset(
|
|
isFollow ? 'collect_red.png'.commonImgPath : 'collect_path.png'.commonImgPath,
|
|
width: 24,
|
|
),
|
|
);
|
|
}
|
|
|
|
//关注样式
|
|
Widget _followView() {
|
|
return InkWell(
|
|
enableFeedback: false,
|
|
onTap: _onFollowTap,
|
|
child: isFollow
|
|
? Container(
|
|
alignment: Alignment.center,
|
|
height: 24,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(3),
|
|
color: Colors.white.withValues(alpha: .05),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
isLoading ? _loading() : const Icon(Icons.check, size: 14, color: Color(0xff989898)),
|
|
4.sizeBoxW,
|
|
const Text(
|
|
'关注',
|
|
style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500, color: Color(0xff989898)),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: Container(
|
|
height: 24,
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0x1AF68804),
|
|
borderRadius: BorderRadius.circular(3),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
isLoading ? _loading() : const Icon(Icons.add, size: 14, color: AppColors.actionRed),
|
|
4.sizeBoxW,
|
|
const Text(
|
|
'关注',
|
|
style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500, color: AppColors.actionRed),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
//文字小说收藏
|
|
Widget _novalView() {
|
|
return InkWell(
|
|
enableFeedback: false,
|
|
onTap: _onCollectTap,
|
|
child: isLoading
|
|
? _loading()
|
|
: Container(
|
|
width: 62,
|
|
height: 24,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: isFollow ? const Color(0xff3D3D3D) : AppColors.actionRed,
|
|
borderRadius: BorderRadius.circular(3),
|
|
),
|
|
child: isFollow
|
|
? Text('已收藏', style: textStyle(10, Colors.white, FontWeight.w400))
|
|
: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Image.asset('add_red.png'.commonImgPath, width: 16, color: Colors.black),
|
|
2.sizeBoxW,
|
|
Text('收藏', style: textStyle(10, Colors.black, FontWeight.w400)),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _loading() => const CupertinoActivityIndicator(color: Colors.white, radius: 8);
|
|
|
|
//加入书架 / 取消收藏
|
|
void _onCollectTap() => isFollow ? _unCollect() : _collect();
|
|
|
|
void _unCollect() async {
|
|
if (isLoading) return;
|
|
setState(() => isLoading = true);
|
|
if (await ACGService.deleteBookshelf(mediaId ?? '')) widget.onSuccess?.call(false);
|
|
isLoading = false;
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
void _collect() async {
|
|
if (isLoading) return;
|
|
setState(() => isLoading = true);
|
|
if (await ACGService.addBookshelf(mediaId ?? '')) widget.onSuccess?.call(true);
|
|
isLoading = false;
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
//关注用户 / 收藏帖子标签
|
|
void _onFollowTap() async {
|
|
if (isLoading) return;
|
|
setState(() => isLoading = true);
|
|
followType == FollowEnum.user ? await _followUser() : await _collectTag();
|
|
isLoading = false;
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
Future _collectTag() async {
|
|
final next = !isFollow;
|
|
if (await MineService.postCollect(mediaId, 'tag', next)) {
|
|
showToast(next ? '关注成功' : '取消关注');
|
|
widget.onSuccess?.call(next);
|
|
}
|
|
}
|
|
|
|
Future _followUser() async {
|
|
final next = !isFollow;
|
|
if (await MineService.getFollow(int.tryParse(mediaId ?? ''), next)) {
|
|
showToast(next ? '关注成功' : '取消关注');
|
|
widget.onSuccess?.call(next);
|
|
}
|
|
}
|
|
}
|