ToneSnap_FSX_Flutter/lib/modules/sideb/widgets/music_item.dart
fengshengxiong da21720c3c 1.首页增加下拉刷新
2.修改下载状态监听方式,实现全局同步
3.修复搜索无结果时页面报错
4.歌单页面点击播放全部和随机是播放当前歌单列表
2024-08-06 15:52:07 +08:00

229 lines
7.1 KiB
Dart

import 'package:background_downloader/background_downloader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/components/dialog/remind_dialog.dart';
import 'package:tone_snap/components/network_image_widget.dart';
import 'package:tone_snap/data/models/music_model.dart';
import 'package:tone_snap/data/storage/offline_box.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/global/download_manager.dart';
import 'package:tone_snap/modules/sideb/controllers/music_player_controller.dart';
import 'package:tone_snap/modules/sideb/more_bottom_sheet/more_bottom_sheet_view.dart';
import 'package:tone_snap/modules/sideb/offline/offline_controller.dart';
import 'package:tone_snap/modules/sideb/personal_music_library/personal_music_library_controller.dart';
import 'package:tone_snap/modules/sideb/widgets/music_item_marquee_text.dart';
import 'package:tone_snap/res/themes/app_colors.dart';
import 'package:tone_snap/utils/obj_util.dart';
class MusicItem extends StatelessWidget {
MusicItem({
super.key,
required this.musicModel,
required this.onTapItem,
this.showDownload = true,
this.showMore = true,
this.showNumber = false,
this.number = 0,
this.playlistModelId,
});
final musicPlayerController = MusicPlayerController.to;
final MusicModel musicModel;
final Function() onTapItem;
final bool showDownload;
final bool showMore;
final bool showNumber;
final int number;
final String? playlistModelId;
@override
Widget build(BuildContext context) {
return Material(
color: Colors.transparent,
child: InkWell(
onTap: onTapItem,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8.h),
child: Row(
children: [
SizedBox(width: 18.w),
if (showNumber) ...[
_buildNumber(),
] else ...[
_buildCover(),
],
SizedBox(width: 12.w),
_buildContent(),
SizedBox(width: 8.w),
if (showDownload) ...[
_buildDownload(),
SizedBox(width: 4.w),
],
if (showMore) ...[
_buildMore(),
SizedBox(width: 4.w),
],
SizedBox(width: 10.w),
],
),
),
),
);
}
Widget _buildNumber() {
return Container(
width: 60.w,
height: 60.w,
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(horizontal: 10).w,
decoration: BoxDecoration(
color: const Color(0xFF242529),
borderRadius: BorderRadius.circular(8).r,
),
child: Text(
number.toString(),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 14.sp,
),
),
);
}
Widget _buildCover() {
return NetworkImageWidget(
url: musicModel.coverUrl,
width: 60.w,
height: 60.w,
radius: 8.r,
);
}
Widget _buildContent() {
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Obx(() {
return MusicItemMarqueeText(
text: musicModel.title,
showMarquee: musicPlayerController.getMusicModel()?.videoId == musicModel.videoId && ObjUtil.isNotEmpty(musicModel.videoId),
);
}),
SizedBox(height: 4.h),
Obx(() {
return MusicItemMarqueeText(
text: musicModel.subtitle,
isTitle: false,
showMarquee: musicPlayerController.getMusicModel()?.videoId == musicModel.videoId && ObjUtil.isNotEmpty(musicModel.videoId),
);
}),
],
),
);
}
Widget _buildDownload() {
return ClipOval(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTapDownload,
child: Padding(
padding: const EdgeInsets.all(4).w,
child: SizedBox(
width: 24.w,
height: 24.w,
child: GetBuilder<DownloadManager>(
id: DownloadManager.to.downloadStateId,
builder: (_) {
if (OfflineBox().checkDownloaded(musicModel.videoId)) {
return Image.asset(Assets.sideBDownloaded);
} else {
if (DownloadManager.to.getMusicModel(musicModel.videoId)?.taskStatus == TaskStatus.enqueued) {
return CircularProgressIndicator(
color: seedColor,
strokeWidth: 2.w,
);
} else if (DownloadManager.to.getMusicModel(musicModel.videoId)?.taskStatus == TaskStatus.running) {
return CircularProgressIndicator(
value: DownloadManager.to.getMusicModel(musicModel.videoId)?.progress,
backgroundColor: seedColor.withOpacity(0.2),
valueColor: const AlwaysStoppedAnimation<Color>(seedColor),
strokeWidth: 2.w,
);
} else {
return Image.asset(Assets.sideBNotDownload2);
}
}
},
),
),
),
),
),
);
}
Widget _buildMore() {
return ClipOval(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTapMore,
child: Padding(
padding: const EdgeInsets.all(4).w,
child: Image.asset(
Assets.sideBMore,
width: 24.w,
height: 24.w,
),
),
),
),
);
}
void onTapDownload() {
if (OfflineBox().checkDownloaded(musicModel.videoId)) {
Get.dialog(
RemindDialog(
content: 'Confirm to remove this song?',
confirmOnTap: () async {
await OfflineBox().delete(musicModel.videoId!);
DownloadManager.to.updateDownloadState();
BaseEasyLoading.toast('Removed');
if (Get.isRegistered<PersonalMusicLibraryController>()) {
PersonalMusicLibraryController.to.refreshOffline();
}
if (Get.isRegistered<OfflineController>()) {
OfflineController.to.getOfflineList();
}
},
),
);
} else {
if (DownloadManager.to.getMusicModel(musicModel.videoId)?.taskStatus == TaskStatus.enqueued
|| DownloadManager.to.getMusicModel(musicModel.videoId)?.taskStatus == TaskStatus.running) {
DownloadManager.to.cancelDownload(musicModel.videoId);
} else {
DownloadManager.to.downloadMusic(musicModel);
}
}
}
void onTapMore() {
Get.bottomSheet(
MoreBottomSheetView(
musicModel: musicModel,
playlistModelId: playlistModelId,
),
);
}
}