fix: 修复了父子组件切换数据不一致的问题

dev
Spark 2 weeks ago
parent acb07b7a7b
commit dc5752b2c6

@ -185,6 +185,18 @@ class DownloadManager extends GetxController {
await _saveDownloadsToPrefs();
}
bool updateSongInfo(int id, bool isCollected, bool isLiked) {
final downloadItem = _downloads[id.toString()];
if (downloadItem != null) {
downloadItem.song.collection = isCollected;
downloadItem.song.likes = isLiked;
_downloads[id.toString()] = downloadItem;
_saveDownloadsToPrefs();
return true;
}
return false;
}
String _getFileExtension(String url) {
// Remove query parameters
final urlWithoutQuery = url.split('?').first;

@ -288,6 +288,14 @@ class _HomeViewState extends State<HomeView> {
builder: (context) => MusicView(
songList: songs,
initialSongIndex: index,
onSongStatusChanged: (index, isCollected, isLiked) {
setState(() {
//
songs[index].collection = isCollected;
songs[index].likes = isLiked;
downloadManager.updateSongInfo(songs[index].id, isCollected, isLiked);
});
},
),
),
);

@ -3,21 +3,23 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:just_audio/just_audio.dart';
import 'package:music_player_miao/common_widget/app_data.dart';
import 'package:music_player_miao/models/universal_bean.dart';
import '../../view_model/home_view_model.dart';
import '../api/api_music_likes.dart';
import '../api/api_collection.dart';
import '../common/download_manager.dart';
import '../common_widget/Song_widegt.dart';
import '../models/universal_bean.dart';
class MusicView extends StatefulWidget {
final List<Song> songList;
final int initialSongIndex;
final Function(int index, bool isCollected, bool isLiked)? onSongStatusChanged;
const MusicView({
super.key,
required this.songList,
required this.initialSongIndex
required this.initialSongIndex,
this.onSongStatusChanged,
});
@override
@ -379,14 +381,23 @@ class _MusicViewState extends State<MusicView> {
Row(
children: [
IconButton(
// TODO BUG
//
onPressed: () async{
setState(() {
likesnot = !likesnot;
likes[currentSongIndex] = !likes[currentSongIndex];
});
await LikesApiMusic().likesMusic(musicId: id[currentSongIndex], Authorization: AppData().currentToken);
UniversalBean response = await LikesApiMusic().likesMusic(musicId: id[currentSongIndex], Authorization: AppData().currentToken);
if (response.code != 200) {
likesnot = !likesnot;
likes[currentSongIndex] = !likes[currentSongIndex];
}
widget.onSongStatusChanged?.call(
currentSongIndex,
collection[currentSongIndex], //
likes[currentSongIndex]
);
},
icon: Image.asset(
likesnot
@ -397,17 +408,23 @@ class _MusicViewState extends State<MusicView> {
),
),
IconButton(
// TODO BUG
//
//
//
// BUG
onPressed: () async{
setState(() {
collectionsnot = !collectionsnot;
collection[currentSongIndex] = !collection[currentSongIndex];
});
await CollectionApiMusic().addCollection(musicId: id[currentSongIndex], Authorization: AppData().currentToken);
UniversalBean response = await CollectionApiMusic().addCollection(musicId: id[currentSongIndex], Authorization: AppData().currentToken);
if (response.code != 200) {
collectionsnot = !collectionsnot;
collection[currentSongIndex] = !collection[currentSongIndex];
}
widget.onSongStatusChanged?.call(
currentSongIndex,
collection[currentSongIndex],
likes[currentSongIndex] //
);
},
icon: Image.asset(
collectionsnot

@ -38,6 +38,7 @@ class _MyDownloadViewState extends State<MyDownloadView> {
void _getSongs() async {
setState(() {
_songs = downloadManager.getLocalSongs();
_selectedItems = List.generate(_songs.length, (index) => false);
});
}
@ -238,6 +239,14 @@ class _MyDownloadViewState extends State<MyDownloadView> {
builder: (context) => MusicView(
songList: _songs,
initialSongIndex: 0,
onSongStatusChanged: (index, isCollected, isLiked) {
setState(() {
//
_songs[index].collection = isCollected;
_songs[index].likes = isLiked;
downloadManager.updateSongInfo(_songs[index].id, isCollected, isLiked);
});
},
),
),
);
@ -316,7 +325,14 @@ class _MyDownloadViewState extends State<MyDownloadView> {
MaterialPageRoute(
builder: (context) => MusicView(
songList: _songs,
initialSongIndex: index, // 使
initialSongIndex: index,
onSongStatusChanged: (index, isCollected, isLiked) {
setState(() {
//
_songs[index].collection = isCollected;
_songs[index].likes = isLiked;
});
},
),
),
);

Loading…
Cancel
Save