From bb36d71c59b47aea63d1878d234051eb493b25c0 Mon Sep 17 00:00:00 2001 From: Ferry-200 Date: Sat, 22 Jun 2024 11:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AD=A3=E5=9C=A8=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E7=95=8C=E9=9D=A2=E5=88=87=E6=AD=8C=E6=97=B6=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=8D=A0=E7=94=A8=E6=BF=80=E5=A2=9E=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../now_playing_page/component/main_view.dart | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/page/now_playing_page/component/main_view.dart b/lib/page/now_playing_page/component/main_view.dart index 471a2e4..fe96e73 100644 --- a/lib/page/now_playing_page/component/main_view.dart +++ b/lib/page/now_playing_page/component/main_view.dart @@ -370,36 +370,50 @@ class NowPlayingTitle extends StatelessWidget { } } -class NowPlayingCover extends StatelessWidget { +class NowPlayingCover extends StatefulWidget { const NowPlayingCover({super.key}); + @override + State createState() => _NowPlayingCoverState(); +} + +class _NowPlayingCoverState extends State { + final playService = PlayService.instance; + Future?>? 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(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!, @@ -410,4 +424,10 @@ class NowPlayingCover extends StatelessWidget { ), ); } + + @override + void dispose() { + playService.removeListener(updateCover); + super.dispose(); + } }