Skip to content

Commit

Permalink
修复正在播放界面切歌时内存占用激增的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferry-200 committed Jun 22, 2024
1 parent 370c9b9 commit bb36d71
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions lib/page/now_playing_page/component/main_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,36 +370,50 @@ class NowPlayingTitle extends StatelessWidget {
}
}

class NowPlayingCover extends StatelessWidget {
class NowPlayingCover extends StatefulWidget {
const NowPlayingCover({super.key});

@override
State<NowPlayingCover> createState() => _NowPlayingCoverState();
}

class _NowPlayingCoverState extends State<NowPlayingCover> {
final playService = PlayService.instance;
Future<ImageProvider<Object>?>? nowPlayingCover;

void updateCover() {
setState(() {
nowPlayingCover = playService.nowPlaying?.largeCover;
});
}

@override
void initState() {
super.initState();
playService.addListener(updateCover);
nowPlayingCover = playService.nowPlaying?.largeCover;
}

@override
Widget build(BuildContext context) {
final playService = Provider.of<PlayService>(context);
final nowPlaying = playService.nowPlaying;
final scheme = Theme.of(context).colorScheme;

final placeHolder = FittedBox(
child: Icon(
Symbols.broken_image,
size: 400.0,
color: scheme.onSecondaryContainer,
),
);

return RepaintBoundary(
child: nowPlaying == null
? FittedBox(
child: Icon(
Symbols.broken_image,
size: 400.0,
color: scheme.onSecondaryContainer,
),
)
child: nowPlayingCover == null
? placeHolder
: FutureBuilder(
future: nowPlaying.largeCover,
future: nowPlayingCover,
builder: (context, snapshot) {
final scheme = Theme.of(context).colorScheme;
if (snapshot.data == null) {
return FittedBox(
child: Icon(
Symbols.broken_image,
size: 400.0,
color: scheme.onSecondaryContainer,
),
);
return placeHolder;
}
return Image(
image: snapshot.data!,
Expand All @@ -410,4 +424,10 @@ class NowPlayingCover extends StatelessWidget {
),
);
}

@override
void dispose() {
playService.removeListener(updateCover);
super.dispose();
}
}

0 comments on commit bb36d71

Please sign in to comment.