|
|
|
@ -20,27 +20,35 @@ class HomeView extends StatefulWidget {
|
|
|
|
|
State<HomeView> createState() => _HomeViewState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _HomeViewState extends State<HomeView> {
|
|
|
|
|
// 使用 GetX 框架的依赖注入,将 HomeViewModel 实例注册为 homeVM。
|
|
|
|
|
// Get.put() 方法会创建 HomeViewModel 的实例,并将其保存在 GetX 的依赖管理器中,方便后续使用。
|
|
|
|
|
class _HomeViewState extends State<HomeView>
|
|
|
|
|
with AutomaticKeepAliveClientMixin {
|
|
|
|
|
final homeVM = Get.put(HomeViewModel());
|
|
|
|
|
|
|
|
|
|
// 创建一个 TextEditingController,用于控制和监听搜索输入框的文本变化。
|
|
|
|
|
// 这个控制器可以用于获取输入框的内容、清空输入框等操作。
|
|
|
|
|
final TextEditingController _controller = TextEditingController();
|
|
|
|
|
|
|
|
|
|
// 定义一个布尔变量 _isSearching,用于表示当前是否处于搜索状态。
|
|
|
|
|
// 当用户在搜索框中输入内容时,_isSearching 会变为 true;当输入框为空时,_isSearching 会变为 false。
|
|
|
|
|
bool _isSearching = false;
|
|
|
|
|
final downloadManager = Get.put(DownloadManager());
|
|
|
|
|
List<Song> selectedSongs = [];
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
bool get wantKeepAlive => true;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_fetchSonglistData();
|
|
|
|
|
}
|
|
|
|
|
List<Song> selectedSongs = [];
|
|
|
|
|
|
|
|
|
|
Future<void> _onRefresh() async {
|
|
|
|
|
try {
|
|
|
|
|
// 重新获取数据
|
|
|
|
|
await _fetchSonglistData();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print('Refresh error: $e');
|
|
|
|
|
// 可以在这里添加错误提示
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _fetchSonglistData() async {
|
|
|
|
|
try {
|
|
|
|
|
MusicListBean bean1 =
|
|
|
|
|
await GetMusic().getMusic1(Authorization: AppData().currentToken);
|
|
|
|
|
MusicListBean bean2 =
|
|
|
|
@ -79,8 +87,10 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
collection: bean3.collectOrNot!),
|
|
|
|
|
];
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print('Error occurred while fetching song list: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///轮播图
|
|
|
|
|
List<Map> imgList = [
|
|
|
|
@ -107,23 +117,32 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
|
|
|
|
|
// 循环处理每个搜索结果,通过 id 请求详细信息
|
|
|
|
|
for (var data in bean.data!) {
|
|
|
|
|
if (data.id != null) { // 确保 id 不为 null
|
|
|
|
|
if (data.id != null) {
|
|
|
|
|
// 确保 id 不为 null
|
|
|
|
|
// 使用每个歌曲的 id 获取详细信息,并返回一个 Future
|
|
|
|
|
songDetailsFutures.add(
|
|
|
|
|
GetMusicDetail().getMusicDetail(
|
|
|
|
|
songDetailsFutures.add(GetMusicDetail()
|
|
|
|
|
.getMusicDetail(
|
|
|
|
|
songId: data.id!,
|
|
|
|
|
Authorization: AppData().currentToken,
|
|
|
|
|
).then((details) {
|
|
|
|
|
)
|
|
|
|
|
.then((details) {
|
|
|
|
|
if (details != null) {
|
|
|
|
|
// 将详细歌曲信息封装成 Song 对象
|
|
|
|
|
return Song(
|
|
|
|
|
artistPic: details.artistPic ?? '', // 歌手封面图
|
|
|
|
|
title: data.name ?? '', // 歌曲名称
|
|
|
|
|
artist: details.artist ?? '', // 歌手名称
|
|
|
|
|
musicurl: details.musicurl ?? '', // 歌曲路径
|
|
|
|
|
pic: details.pic ?? '', // 封面图片路径
|
|
|
|
|
id: details.id, // 歌曲 ID
|
|
|
|
|
likes: details.likes, // 是否喜欢
|
|
|
|
|
artistPic: details.artistPic ?? '',
|
|
|
|
|
// 歌手封面图
|
|
|
|
|
title: data.name ?? '',
|
|
|
|
|
// 歌曲名称
|
|
|
|
|
artist: details.artist ?? '',
|
|
|
|
|
// 歌手名称
|
|
|
|
|
musicurl: details.musicurl ?? '',
|
|
|
|
|
// 歌曲路径
|
|
|
|
|
pic: details.pic ?? '',
|
|
|
|
|
// 封面图片路径
|
|
|
|
|
id: details.id,
|
|
|
|
|
// 歌曲 ID
|
|
|
|
|
likes: details.likes,
|
|
|
|
|
// 是否喜欢
|
|
|
|
|
collection: details.collection, // 是否收藏
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
@ -131,8 +150,7 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
}).catchError((error) {
|
|
|
|
|
print("Error occurred while fetching song details: $error");
|
|
|
|
|
return null; // 异常处理,返回 null
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}));
|
|
|
|
|
} else {
|
|
|
|
|
print("Song ID is null for song: ${data.name}");
|
|
|
|
|
}
|
|
|
|
@ -142,7 +160,10 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
List<Song?> songDetailsList = await Future.wait(songDetailsFutures);
|
|
|
|
|
|
|
|
|
|
// 过滤掉 null 值
|
|
|
|
|
List<Song> validSongDetails = songDetailsList.where((song) => song != null).cast<Song>().toList();
|
|
|
|
|
List<Song> validSongDetails = songDetailsList
|
|
|
|
|
.where((song) => song != null)
|
|
|
|
|
.cast<Song>()
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
// 最后更新 UI,一次性更新 _filteredData
|
|
|
|
|
setState(() {
|
|
|
|
@ -175,6 +196,8 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
super.build(context);
|
|
|
|
|
|
|
|
|
|
///轮播图
|
|
|
|
|
var MySwiperWidget = Swiper(
|
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
@ -205,9 +228,7 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
child: Scaffold(
|
|
|
|
|
resizeToAvoidBottomInset: false,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
body: SingleChildScrollView(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
body: Column(
|
|
|
|
|
children: [
|
|
|
|
|
///头部
|
|
|
|
|
Container(
|
|
|
|
@ -217,24 +238,21 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'喵听',
|
|
|
|
|
style:
|
|
|
|
|
TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: 10,
|
|
|
|
|
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(width: 10),
|
|
|
|
|
Text(
|
|
|
|
|
'你的云端音乐库',
|
|
|
|
|
style:
|
|
|
|
|
TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 10,),
|
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
|
|
|
|
|
|
///搜索
|
|
|
|
|
Container(
|
|
|
|
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 10),
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
|
|
|
|
|
child: Column(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
@ -305,8 +323,10 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
// 创建一个新的页面(MusicView),并将当前歌曲和索引作为参数传递给它
|
|
|
|
|
builder: (context) => MusicView(
|
|
|
|
|
songList: _filteredData, // 传递当前列表项对应的歌曲对象,包含歌曲的详细信息
|
|
|
|
|
initialSongIndex: index, // 传递当前歌曲在歌曲列表中的索引,用于在新页面中显示或操作
|
|
|
|
|
songList: _filteredData,
|
|
|
|
|
// 传递当前列表项对应的歌曲对象,包含歌曲的详细信息
|
|
|
|
|
initialSongIndex:
|
|
|
|
|
index, // 传递当前歌曲在歌曲列表中的索引,用于在新页面中显示或操作
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
@ -319,18 +339,35 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
const SizedBox(height: 10,),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 10,
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
///推荐+轮播图
|
|
|
|
|
Expanded(
|
|
|
|
|
child: RefreshIndicator(
|
|
|
|
|
onRefresh: _onRefresh,
|
|
|
|
|
color: const Color(0xff429482),
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
///推荐+轮播图
|
|
|
|
|
Container(
|
|
|
|
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 10),
|
|
|
|
|
padding:
|
|
|
|
|
const EdgeInsets.only(left: 20, right: 20, top: 10),
|
|
|
|
|
child: const Text(
|
|
|
|
|
'每日推荐',
|
|
|
|
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 5,),
|
|
|
|
|
const SizedBox(height: 5),
|
|
|
|
|
Container(
|
|
|
|
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 5),
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 20, vertical: 5),
|
|
|
|
|
height: 186,
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
child: MySwiperWidget,
|
|
|
|
@ -341,13 +378,17 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
///精选歌曲
|
|
|
|
|
Container(
|
|
|
|
|
alignment: Alignment.topLeft,
|
|
|
|
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 5),
|
|
|
|
|
padding:
|
|
|
|
|
const EdgeInsets.only(left: 20, right: 20, top: 5),
|
|
|
|
|
child: const Text(
|
|
|
|
|
'精选歌曲',
|
|
|
|
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 5,),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 5,
|
|
|
|
|
),
|
|
|
|
|
ListView.builder(
|
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
|
itemCount: selectedSongs.length,
|
|
|
|
@ -358,11 +399,13 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
leading: Image.network(selectedSongs[index].pic),
|
|
|
|
|
title: Text(
|
|
|
|
|
selectedSongs[index].title,
|
|
|
|
|
style: const TextStyle(fontSize: 18, color: Colors.black),
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 18, color: Colors.black),
|
|
|
|
|
),
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
selectedSongs[index].artist,
|
|
|
|
|
style: const TextStyle(fontSize: 16, color: Colors.black),
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 16, color: Colors.black),
|
|
|
|
|
),
|
|
|
|
|
trailing: InkWell(
|
|
|
|
|
onTap: () {
|
|
|
|
@ -377,12 +420,17 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
builder: (context) => MusicView(
|
|
|
|
|
songList: selectedSongs,
|
|
|
|
|
initialSongIndex: index,
|
|
|
|
|
onSongStatusChanged: (index, isCollected, isLiked) {
|
|
|
|
|
onSongStatusChanged:
|
|
|
|
|
(index, isCollected, isLiked) {
|
|
|
|
|
setState(() {
|
|
|
|
|
// 更新父组件中的数据
|
|
|
|
|
selectedSongs[index].collection = isCollected;
|
|
|
|
|
selectedSongs[index].collection =
|
|
|
|
|
isCollected;
|
|
|
|
|
selectedSongs[index].likes = isLiked;
|
|
|
|
|
downloadManager.updateSongInfo(selectedSongs[index].id, isCollected, isLiked);
|
|
|
|
|
downloadManager.updateSongInfo(
|
|
|
|
|
selectedSongs[index].id,
|
|
|
|
|
isCollected,
|
|
|
|
|
isLiked);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
@ -393,16 +441,23 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
const SizedBox(height: 10,),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 10,
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
///精选歌单
|
|
|
|
|
Container(
|
|
|
|
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 5),
|
|
|
|
|
padding:
|
|
|
|
|
const EdgeInsets.only(left: 20, right: 20, top: 5),
|
|
|
|
|
child: const Text(
|
|
|
|
|
'精选歌单',
|
|
|
|
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 5,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 5,),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 180,
|
|
|
|
|
child: ListView.builder(
|
|
|
|
@ -423,6 +478,10 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -433,7 +492,8 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.vertical(top: Radius.circular(30)),
|
|
|
|
|
),
|
|
|
|
|
builder: (context) => StatefulBuilder( // 使用StatefulBuilder以便动态修改状态
|
|
|
|
|
builder: (context) => StatefulBuilder(
|
|
|
|
|
// 使用StatefulBuilder以便动态修改状态
|
|
|
|
|
builder: (context, setState) {
|
|
|
|
|
bool likesnot = false; // 初始状态,假设未点赞
|
|
|
|
|
|
|
|
|
@ -479,11 +539,11 @@ class _HomeViewState extends State<HomeView> {
|
|
|
|
|
Column(
|
|
|
|
|
children: [
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: (){},
|
|
|
|
|
onPressed: () {},
|
|
|
|
|
icon: Image.asset("assets/img/list_good.png"),
|
|
|
|
|
iconSize: 60,
|
|
|
|
|
),
|
|
|
|
|
Text("点赞")
|
|
|
|
|
const Text("点赞")
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
Column(
|
|
|
|
|