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

dev
Spark 3 weeks ago
parent acb07b7a7b
commit dc5752b2c6

@ -185,6 +185,18 @@ class DownloadManager extends GetxController {
await _saveDownloadsToPrefs(); 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) { String _getFileExtension(String url) {
// Remove query parameters // Remove query parameters
final urlWithoutQuery = url.split('?').first; final urlWithoutQuery = url.split('?').first;

@ -288,6 +288,14 @@ class _HomeViewState extends State<HomeView> {
builder: (context) => MusicView( builder: (context) => MusicView(
songList: songs, songList: songs,
initialSongIndex: index, 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:get/get.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';
import 'package:music_player_miao/common_widget/app_data.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 '../../view_model/home_view_model.dart';
import '../api/api_music_likes.dart'; import '../api/api_music_likes.dart';
import '../api/api_collection.dart'; import '../api/api_collection.dart';
import '../common/download_manager.dart'; import '../common/download_manager.dart';
import '../common_widget/Song_widegt.dart'; import '../common_widget/Song_widegt.dart';
import '../models/universal_bean.dart';
class MusicView extends StatefulWidget { class MusicView extends StatefulWidget {
final List<Song> songList; final List<Song> songList;
final int initialSongIndex; final int initialSongIndex;
final Function(int index, bool isCollected, bool isLiked)? onSongStatusChanged;
const MusicView({ const MusicView({
super.key, super.key,
required this.songList, required this.songList,
required this.initialSongIndex required this.initialSongIndex,
this.onSongStatusChanged,
}); });
@override @override
@ -379,14 +381,23 @@ class _MusicViewState extends State<MusicView> {
Row( Row(
children: [ children: [
IconButton( IconButton(
// TODO BUG
//
onPressed: () async{ onPressed: () async{
setState(() { setState(() {
likesnot = !likesnot; likesnot = !likesnot;
likes[currentSongIndex] = !likes[currentSongIndex]; 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( icon: Image.asset(
likesnot likesnot
@ -397,17 +408,23 @@ class _MusicViewState extends State<MusicView> {
), ),
), ),
IconButton( IconButton(
// TODO BUG
//
//
//
// BUG
onPressed: () async{ onPressed: () async{
setState(() { setState(() {
collectionsnot = !collectionsnot; collectionsnot = !collectionsnot;
collection[currentSongIndex] = !collection[currentSongIndex]; 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( icon: Image.asset(
collectionsnot collectionsnot

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

Loading…
Cancel
Save