Skip to content

Commit 70804d6

Browse files
committed
feat(song_bar): add play count display option in song bar
1 parent d8793eb commit 70804d6

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

lib/screens/home_page.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,14 @@ class _HomePageState extends State<HomePage> {
226226
final song = Map<String, dynamic>.from(
227227
mostPlayedSongs[index],
228228
);
229-
final plays = (song['listeningCount'] is int)
230-
? song['listeningCount'] as int
231-
: int.tryParse(
232-
song['listeningCount']?.toString() ?? '',
233-
) ??
234-
0;
235-
236-
song['artist'] = '${song['artist'] ?? ''} • $plays plays';
237229

238230
return RepaintBoundary(
239231
key: ValueKey('most_played_${song['ytid']}_$index'),
240232
child: SongBar(
241233
song,
242234
true,
243235
borderRadius: borderRadius,
236+
showPlayTime: true,
244237
onPlay: () => audioHandler.addPlaylistToQueue([
245238
mostPlayedSongs[index],
246239
], replace: true),

lib/widgets/song_bar.dart

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class SongBar extends StatefulWidget {
4848
this.borderRadius = BorderRadius.zero,
4949
this.isFromLikedSongs = false,
5050
this.showQueueActions = true,
51+
this.showPlayTime = false,
5152
this.playlistId,
5253
this.onRenamed,
5354
super.key,
@@ -60,6 +61,7 @@ class SongBar extends StatefulWidget {
6061
final VoidCallback? onPlay;
6162
final bool? isRecentSong;
6263
final bool showMusicDuration;
64+
final bool showPlayTime;
6365
final BorderRadius borderRadius;
6466
final bool isFromLikedSongs;
6567
final bool showQueueActions;
@@ -127,6 +129,12 @@ class _SongBarState extends State<SongBar> {
127129
@override
128130
Widget build(BuildContext context) {
129131
final colorScheme = Theme.of(context).colorScheme;
132+
final _plays = widget.showPlayTime
133+
? (widget.song['listeningCount'] is int)
134+
? widget.song['listeningCount'] as int
135+
: int.tryParse(widget.song['listeningCount']?.toString() ?? '') ??
136+
0
137+
: null;
130138

131139
return Padding(
132140
padding: commonBarPadding,
@@ -147,6 +155,7 @@ class _SongBarState extends State<SongBar> {
147155
child: _SongInfo(
148156
title: _songTitle,
149157
artist: _songArtist,
158+
plays: _plays,
150159
colorScheme: colorScheme,
151160
),
152161
),
@@ -526,11 +535,13 @@ class _SongInfo extends StatelessWidget {
526535
const _SongInfo({
527536
required this.title,
528537
required this.artist,
538+
this.plays,
529539
required this.colorScheme,
530540
});
531541

532542
final String title;
533543
final String artist;
544+
final int? plays;
534545
final ColorScheme colorScheme;
535546

536547
@override
@@ -548,14 +559,46 @@ class _SongInfo extends StatelessWidget {
548559
),
549560
),
550561
const SizedBox(height: 2),
551-
Text(
552-
artist,
553-
overflow: TextOverflow.ellipsis,
554-
style: TextStyle(
555-
fontWeight: FontWeight.w400,
556-
fontSize: 13,
557-
color: colorScheme.onSurfaceVariant,
558-
),
562+
Row(
563+
children: [
564+
Flexible(
565+
child: Text(
566+
artist,
567+
overflow: TextOverflow.ellipsis,
568+
style: TextStyle(
569+
fontWeight: FontWeight.w400,
570+
fontSize: 13,
571+
color: colorScheme.onSurfaceVariant,
572+
),
573+
),
574+
),
575+
if (plays != null && plays! > 0) ...[
576+
Padding(
577+
padding: const EdgeInsets.symmetric(horizontal: 4),
578+
child: Text(
579+
'•',
580+
style: TextStyle(
581+
fontSize: 13,
582+
color: colorScheme.onSurfaceVariant,
583+
),
584+
),
585+
),
586+
Icon(
587+
FluentIcons.headphones_20_regular,
588+
size: 12,
589+
color: colorScheme.primary,
590+
),
591+
const SizedBox(width: 3),
592+
Text(
593+
'$plays',
594+
style: TextStyle(
595+
fontSize: 12,
596+
fontWeight: FontWeight.w600,
597+
color: colorScheme.primary,
598+
),
599+
),
600+
],
601+
],
559602
),
560603
],
561604
);

0 commit comments

Comments
 (0)