diff --git a/lib/customWidgets/song_bar.dart b/lib/customWidgets/song_bar.dart index 932d3e4bb..9bd980918 100644 --- a/lib/customWidgets/song_bar.dart +++ b/lib/customWidgets/song_bar.dart @@ -4,15 +4,13 @@ import 'package:musify/API/musify.dart'; import 'package:musify/services/audio_manager.dart'; import 'package:musify/style/appColors.dart'; -class SongBar extends StatefulWidget { - final dynamic song; - const SongBar({Key? key, required this.song}) : super(key: key); +class SongBar extends StatelessWidget { + SongBar(this.song); - @override - State createState() => _SongBarState(); -} + late final dynamic song; + late final songLikeStatus = + ValueNotifier(isSongAlreadyLiked(song["ytid"])); -class _SongBarState extends State { @override Widget build(BuildContext context) { return Card( @@ -24,7 +22,7 @@ class _SongBarState extends State { child: InkWell( borderRadius: BorderRadius.circular(20.0), onTap: () { - playSong((widget.song)); + playSong((song)); }, splashColor: accent, hoverColor: accent, @@ -42,7 +40,7 @@ class _SongBarState extends State { ), ), title: Text( - ((widget.song)['title']) + ((song)['title']) .toString() .split("(")[0] .replaceAll(""", "\"") @@ -50,26 +48,35 @@ class _SongBarState extends State { style: TextStyle(color: accent), ), subtitle: Text( - widget.song['more_info']["singers"], + song['more_info']["singers"], style: TextStyle(color: accentLight), ), trailing: Row(mainAxisSize: MainAxisSize.min, children: [ - IconButton( - color: accent, - icon: isSongAlreadyLiked((widget.song)['ytid']) - ? Icon(MdiIcons.star) - : Icon(MdiIcons.starOutline), - onPressed: () => { - setState(() { - isSongAlreadyLiked((widget.song)['ytid']) - ? removeUserLikedSong((widget.song)['ytid']) - : addUserLikedSong((widget.song)['ytid']); - }) - }), + ValueListenableBuilder( + valueListenable: songLikeStatus, + builder: (_, value, __) { + if (value == true) { + return IconButton( + color: accent, + icon: Icon(MdiIcons.star), + onPressed: () => { + removeUserLikedSong((song)['ytid']), + songLikeStatus.value = false + }); + } else { + return IconButton( + color: accent, + icon: Icon(MdiIcons.starOutline), + onPressed: () => { + addUserLikedSong((song)['ytid']), + songLikeStatus.value = true + }); + } + }), IconButton( color: accent, icon: Icon(MdiIcons.downloadOutline), - onPressed: () => downloadSong((widget.song)), + onPressed: () => downloadSong((song)), ), ]), ) diff --git a/lib/ui/homePage.dart b/lib/ui/homePage.dart index b3d7bf864..a15188e41 100644 --- a/lib/ui/homePage.dart +++ b/lib/ui/homePage.dart @@ -123,8 +123,7 @@ class _HomePageState extends State { (data as dynamic).data.length, itemBuilder: (context, index) { return SongBar( - song: (data as dynamic) - .data[index]); + (data as dynamic).data[index]); }))) ], ), diff --git a/lib/ui/player.dart b/lib/ui/player.dart index a5c116122..bd7de8911 100644 --- a/lib/ui/player.dart +++ b/lib/ui/player.dart @@ -23,6 +23,8 @@ get isPlaying => buttonNotifier.value == MPlayerState.playing; get isPaused => buttonNotifier.value == MPlayerState.paused; +final songLikeStatus = ValueNotifier(isSongAlreadyLiked(ytid)); + enum MPlayerState { stopped, playing, paused, loading } class AudioApp extends StatefulWidget { @@ -305,19 +307,29 @@ class AudioAppState extends State { changeLoopStatus(); }, ), - IconButton( - color: accent, - icon: isSongAlreadyLiked(ytid) - ? Icon(MdiIcons.star) - : Icon(MdiIcons.starOutline), - iconSize: size.width * 0.056, - onPressed: () => { - setState(() { - isSongAlreadyLiked(ytid) - ? removeUserLikedSong(ytid) - : addUserLikedSong(ytid); - }) - }), + ValueListenableBuilder( + valueListenable: songLikeStatus, + builder: (_, value, __) { + if (value == true) { + return IconButton( + color: accent, + icon: Icon(MdiIcons.star), + iconSize: size.width * 0.056, + onPressed: () => { + removeUserLikedSong(ytid), + songLikeStatus.value = false + }); + } else { + return IconButton( + color: accent, + icon: Icon(MdiIcons.starOutline), + iconSize: size.width * 0.056, + onPressed: () => { + addUserLikedSong(ytid), + songLikeStatus.value = true + }); + } + }), ], ), ), diff --git a/lib/ui/playlistPage.dart b/lib/ui/playlistPage.dart index 611d7d99c..3294a892f 100644 --- a/lib/ui/playlistPage.dart +++ b/lib/ui/playlistPage.dart @@ -229,8 +229,7 @@ class _PlaylistPageState extends State { return Padding( padding: const EdgeInsets.only( top: 5, bottom: 5), - child: SongBar( - song: _songsList[index])); + child: SongBar(_songsList[index])); }) : Align( alignment: Alignment.center, diff --git a/lib/ui/searchPage.dart b/lib/ui/searchPage.dart index 3b8a69345..9430d941a 100644 --- a/lib/ui/searchPage.dart +++ b/lib/ui/searchPage.dart @@ -98,7 +98,7 @@ class _SearchPageState extends State { itemBuilder: (BuildContext ctxt, int index) { return Padding( padding: const EdgeInsets.only(top: 5, bottom: 5), - child: SongBar(song: searchedList[index])); + child: SongBar(searchedList[index])); }, ) ], diff --git a/lib/ui/userLikedSongsPage.dart b/lib/ui/userLikedSongsPage.dart index a4d130e9f..53d00c713 100644 --- a/lib/ui/userLikedSongsPage.dart +++ b/lib/ui/userLikedSongsPage.dart @@ -130,7 +130,7 @@ class _UserLikedSongsState extends State { itemBuilder: (BuildContext context, int index) { return Padding( padding: const EdgeInsets.only(top: 5, bottom: 5), - child: SongBar(song: userLikedSongsList[index])); + child: SongBar(userLikedSongsList[index])); }) ], )