Ohne Eintrittspunkt wären Playlisten unbenutzbar, da sie sonst nie Songs
erhalten könnten. SongList bekommt einen zusätzlichen Trailing-Button
(Icons.playlist_add) neben dem Favoriten-Herz, der ein Bottom-Sheet mit
allen Playlisten öffnet ("Neue Playlist" oben, dann bestehende). Antippen
hängt den Song ans Ende der gewählten Playlist an und zeigt eine SnackBar.
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
117 lines
4.2 KiB
Dart
117 lines
4.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../player/audio_handler.dart';
|
|
import '../playlists/create_playlist_dialog.dart';
|
|
import '../shared/cover.dart';
|
|
import '../shared/favorite_button.dart';
|
|
import 'database.dart';
|
|
import 'playlist_service.dart';
|
|
import 'song_media.dart';
|
|
|
|
/// Scrollbare Songliste; Tippen spielt die ganze Liste ab dem Song ab.
|
|
class SongList extends StatelessWidget {
|
|
const SongList(this.songs, {super.key});
|
|
final List<Song> songs;
|
|
|
|
/// Öffnet ein Bottom-Sheet mit allen Playlisten, um [song] hinzuzufügen.
|
|
Future<void> _showAddToPlaylist(BuildContext context, Song song) async {
|
|
final db = context.read<MeloDb>();
|
|
final service = context.read<PlaylistService>();
|
|
await showModalBottomSheet(
|
|
context: context,
|
|
builder: (sheetContext) => SafeArea(
|
|
child: StreamBuilder<List<Playlist>>(
|
|
stream: db.watchPlaylists(),
|
|
builder: (context, snapshot) {
|
|
final playlists = snapshot.data ?? const [];
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
ListTile(
|
|
leading: const Icon(Icons.add),
|
|
title: const Text('Neue Playlist'),
|
|
onTap: () async {
|
|
final newId = await showDialog<String?>(
|
|
context: sheetContext,
|
|
builder: (_) => const CreatePlaylistDialog(),
|
|
);
|
|
if (newId == null) return;
|
|
final position = (await db.watchPlaylistSongs(newId).first).length;
|
|
await service.addSongToPlaylist(newId, song.id, position);
|
|
if (sheetContext.mounted) {
|
|
Navigator.pop(sheetContext);
|
|
ScaffoldMessenger.of(sheetContext).showSnackBar(
|
|
const SnackBar(content: Text('Zu Playlist hinzugefügt')),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
for (final playlist in playlists)
|
|
ListTile(
|
|
leading: const Icon(Icons.playlist_play),
|
|
title: Text(playlist.name),
|
|
onTap: () async {
|
|
final position =
|
|
(await db.watchPlaylistSongs(playlist.id).first).length;
|
|
await service.addSongToPlaylist(playlist.id, song.id, position);
|
|
if (sheetContext.mounted) {
|
|
Navigator.pop(sheetContext);
|
|
ScaffoldMessenger.of(sheetContext).showSnackBar(
|
|
SnackBar(content: Text('Zu ${playlist.name} hinzugefügt')),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final handler = context.read<MeloAudioHandler>();
|
|
return ListView.builder(
|
|
itemCount: songs.length,
|
|
itemBuilder: (context, i) {
|
|
final s = songs[i];
|
|
return ListTile(
|
|
leading: CoverImage(
|
|
artUri: s.coverPath != null ? Uri.file(s.coverPath!) : null,
|
|
size: 48,
|
|
radius: 6,
|
|
),
|
|
title: Text(s.title, maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
subtitle: Text(s.artist ?? 'Unbekannt',
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
FavoriteButton(songId: s.id),
|
|
IconButton(
|
|
tooltip: 'Zu Playlist hinzufügen',
|
|
icon: const Icon(Icons.playlist_add),
|
|
onPressed: () => _showAddToPlaylist(context, s),
|
|
),
|
|
],
|
|
),
|
|
onTap: () async {
|
|
try {
|
|
await playSongs(handler, songs, i);
|
|
} catch (e) {
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Wiedergabe fehlgeschlagen: $e')),
|
|
);
|
|
}
|
|
}
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|