RhythmBox/lib/widgets/tracks/playlist_track_list.dart

85 lines
2.4 KiB
Dart
Raw Normal View History

2024-08-26 12:26:07 +00:00
import 'package:flutter/material.dart';
import 'package:get/get.dart';
2024-08-26 17:49:05 +00:00
import 'package:rhythm_box/providers/audio_player.dart';
2024-08-26 12:26:07 +00:00
import 'package:rhythm_box/providers/spotify.dart';
import 'package:rhythm_box/widgets/auto_cache_image.dart';
import 'package:skeletonizer/skeletonizer.dart';
import 'package:spotify/spotify.dart';
2024-08-27 06:35:16 +00:00
import 'package:rhythm_box/services/artist.dart';
2024-08-26 12:26:07 +00:00
class PlaylistTrackList extends StatefulWidget {
final String playlistId;
const PlaylistTrackList({super.key, required this.playlistId});
@override
State<PlaylistTrackList> createState() => _PlaylistTrackListState();
}
class _PlaylistTrackListState extends State<PlaylistTrackList> {
late final SpotifyProvider _spotify = Get.find();
bool _isLoading = true;
List<Track>? _tracks;
Future<void> _pullTracks() async {
_tracks = (await _spotify.api.playlists
.getTracksByPlaylistId(widget.playlistId)
.all())
.toList();
setState(() => _isLoading = false);
}
@override
void initState() {
_pullTracks();
super.initState();
}
@override
Widget build(BuildContext context) {
return Skeletonizer.sliver(
enabled: _isLoading,
child: SliverList.builder(
itemCount: _tracks?.length ?? 3,
itemBuilder: (context, idx) {
final item = _tracks?[idx];
return ListTile(
leading: ClipRRect(
2024-08-27 06:35:16 +00:00
borderRadius: const BorderRadius.all(Radius.circular(8)),
2024-08-26 12:26:07 +00:00
child: item != null
? AutoCacheImage(
item.album!.images!.first.url!,
width: 64.0,
height: 64.0,
)
: const SizedBox(
width: 64,
height: 64,
child: Center(
child: Icon(Icons.image),
),
),
),
title: Text(item?.name ?? 'Loading...'),
subtitle: Text(
2024-08-27 06:35:16 +00:00
item?.artists?.asString() ?? 'Please stand by...',
2024-08-27 12:49:48 +00:00
maxLines: 1,
overflow: TextOverflow.ellipsis,
2024-08-26 12:26:07 +00:00
),
2024-08-26 17:49:05 +00:00
onTap: () {
if (item == null) return;
Get.find<AudioPlayerProvider>().load(
_tracks!,
initialIndex: idx,
autoPlay: true,
);
2024-08-26 17:49:05 +00:00
},
2024-08-26 12:26:07 +00:00
);
},
),
);
}
}