spotify integration works

This commit is contained in:
Leon Astner 2025-08-02 04:48:34 +02:00
parent f70fe3cdd1
commit 025eee7644
11 changed files with 1637 additions and 717 deletions

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import '../services/spotify_service.dart';
import 'package:provider/provider.dart';
import '../services/music_queue_service.dart';
import '../models/spotify_track.dart';
class VotingPage extends StatefulWidget {
@ -11,492 +12,358 @@ class VotingPage extends StatefulWidget {
class _VotingPageState extends State<VotingPage> {
final TextEditingController _searchController = TextEditingController();
final SpotifyService _spotifyService = SpotifyService();
List<Map<String, dynamic>> suggestedSongs = [
{
"id": "1",
"title": "Ocean Breeze",
"artist": "Lofi Dreams",
"votes": 12,
"userVoted": false,
"duration": "3:45",
"imageUrl": "https://i.scdn.co/image/ocean"
},
{
"id": "2",
"title": "Sunset Melody",
"artist": "Acoustic Soul",
"votes": 8,
"userVoted": true,
"duration": "4:12",
"imageUrl": "https://i.scdn.co/image/sunset"
},
{
"id": "3",
"title": "Peaceful Waters",
"artist": "Nature Sounds",
"votes": 5,
"userVoted": false,
"duration": "3:20",
"imageUrl": "https://i.scdn.co/image/water"
},
{
"id": "4",
"title": "Summer Nights",
"artist": "Chill Vibes",
"votes": 3,
"userVoted": false,
"duration": "3:55",
"imageUrl": "https://i.scdn.co/image/night"
},
];
List<SpotifyTrack> _searchResults = [];
bool _isLoading = false;
String _statusMessage = '';
List<SpotifyTrack> searchResults = [];
bool isSearching = false;
@override
void initState() {
super.initState();
_loadInitialQueue();
}
Future<void> _loadInitialQueue() async {
final queueService = Provider.of<MusicQueueService>(context, listen: false);
await queueService.initializeQueue();
}
Future<void> _searchSpotify(String query) async {
if (query.isEmpty) {
setState(() {
searchResults = [];
isSearching = false;
});
return;
}
if (query.isEmpty) return;
setState(() {
isSearching = true;
_isLoading = true;
_statusMessage = 'Searching for "$query"...';
});
try {
final tracks = await _spotifyService.searchTracks(query, limit: 10);
final queueService = Provider.of<MusicQueueService>(context, listen: false);
final results = await queueService.searchTracks(query);
setState(() {
searchResults = tracks;
isSearching = false;
_searchResults = results;
_isLoading = false;
_statusMessage = results.isEmpty
? 'No tracks found for "$query"'
: 'Found ${results.length} tracks';
});
} catch (e) {
print('Error searching Spotify: $e');
setState(() {
searchResults = [];
isSearching = false;
_isLoading = false;
_statusMessage = 'Search failed: ${e.toString()}';
_searchResults = [];
});
}
}
void _upvote(int index) {
setState(() {
suggestedSongs[index]["votes"]++;
if (!suggestedSongs[index]["userVoted"]) {
suggestedSongs[index]["userVoted"] = true;
}
// Sort by votes to update queue order
suggestedSongs.sort((a, b) => b["votes"].compareTo(a["votes"]));
});
}
void _downvote(int index) {
setState(() {
if (suggestedSongs[index]["votes"] > 0) {
suggestedSongs[index]["votes"]--;
}
// Sort by votes to update queue order
suggestedSongs.sort((a, b) => b["votes"].compareTo(a["votes"]));
});
}
void _addToQueue(SpotifyTrack track) {
setState(() {
suggestedSongs.insert(0, {
"id": track.id,
"title": track.name,
"artist": track.artistNames,
"votes": 1,
"userVoted": true,
"duration": track.duration,
"imageUrl": track.imageUrl,
});
});
final queueService = Provider.of<MusicQueueService>(context, listen: false);
queueService.addToQueue(track);
// Show confirmation
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Added "${track.name}" to queue!'),
backgroundColor: const Color(0xFF6366F1),
content: Text('Added "${track.name}" to queue'),
duration: const Duration(seconds: 2),
backgroundColor: const Color(0xFF6366F1),
),
);
// Clear search
_searchController.clear();
setState(() {
searchResults = [];
});
}
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xFF121212),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Search Section
const Text(
'Suggest a Song',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 15),
// Search Bar
Container(
decoration: BoxDecoration(
color: const Color(0xFF1E1E1E),
borderRadius: BorderRadius.circular(12),
),
child: TextField(
controller: _searchController,
style: const TextStyle(color: Colors.white),
decoration: const InputDecoration(
hintText: 'Search for songs, artists...',
hintStyle: TextStyle(color: Colors.grey),
prefixIcon: Icon(Icons.search, color: Color(0xFF6366F1)),
border: InputBorder.none,
contentPadding: EdgeInsets.all(16),
),
onChanged: (value) {
// Search Spotify when user types
_searchSpotify(value);
},
),
),
// Search Results (shown when typing)
if (_searchController.text.isNotEmpty || searchResults.isNotEmpty) ...[
const SizedBox(height: 15),
Row(
children: [
const Text(
'Search Results',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
if (isSearching) ...[
const SizedBox(width: 10),
const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6366F1)),
),
),
],
],
),
const SizedBox(height: 10),
Container(
height: 200,
child: searchResults.isEmpty && !isSearching
? const Center(
child: Text(
'No tracks found',
style: TextStyle(color: Colors.grey),
),
)
: ListView.builder(
itemCount: searchResults.length,
itemBuilder: (context, index) {
final track = searchResults[index];
return Container(
margin: const EdgeInsets.only(bottom: 8),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: const Color(0xFF1E1E1E),
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: const Color(0xFF6366F1).withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: track.imageUrl.isNotEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
track.imageUrl,
width: 40,
height: 40,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return const Icon(
Icons.music_note,
color: Color(0xFF6366F1),
size: 20,
);
},
),
)
: const Icon(
Icons.music_note,
color: Color(0xFF6366F1),
size: 20,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
track.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
Text(
"${track.artistNames}${track.duration}",
style: const TextStyle(
color: Colors.grey,
fontSize: 12,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
ElevatedButton(
onPressed: () => _addToQueue(track),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF6366F1),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
minimumSize: const Size(60, 30),
),
child: const Text(
'Add',
style: TextStyle(fontSize: 12),
),
),
],
),
);
},
),
),
],
const SizedBox(height: 20),
// Voting Queue Section
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
return Consumer<MusicQueueService>(
builder: (context, queueService, child) {
return Scaffold(
backgroundColor: const Color(0xFF121212),
body: SafeArea(
child: Column(
children: [
const Text(
'Community Queue',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
// Header with Search
Container(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Voting',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
// Status indicator
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: const Color(0xFF6366F1).withOpacity(0.2),
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: const Color(0xFF6366F1),
width: 1,
),
),
child: const Text(
'🎵 Spotify',
style: TextStyle(
color: Color(0xFF6366F1),
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
],
),
const SizedBox(height: 20),
// Search Bar
TextField(
controller: _searchController,
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
hintText: 'Search for songs, artists, albums...',
hintStyle: const TextStyle(color: Colors.grey),
prefixIcon: const Icon(Icons.search, color: Colors.grey),
suffixIcon: _isLoading
? const Padding(
padding: EdgeInsets.all(12),
child: SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6366F1)),
),
),
)
: null,
filled: true,
fillColor: const Color(0xFF1E1E1E),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide.none,
),
),
onSubmitted: _searchSpotify,
),
// Status Message
if (_statusMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
_statusMessage,
style: const TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
),
],
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: const Color(0xFF6366F1).withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'↑↓ Vote to reorder',
style: TextStyle(
color: Color(0xFF6366F1),
fontSize: 11,
fontWeight: FontWeight.w500,
// Search Results and Queue
Expanded(
child: DefaultTabController(
length: 2,
child: Column(
children: [
const TabBar(
labelColor: Color(0xFF6366F1),
unselectedLabelColor: Colors.grey,
indicatorColor: Color(0xFF6366F1),
tabs: [
Tab(text: 'Search Results'),
Tab(text: 'Queue'),
],
),
Expanded(
child: TabBarView(
children: [
// Search Results Tab
_buildSearchResults(),
// Queue Tab
_buildQueueView(queueService),
],
),
),
],
),
),
),
],
),
const SizedBox(height: 15),
),
);
},
);
}
Widget _buildSearchResults() {
if (_searchResults.isEmpty && !_isLoading) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.search,
size: 80,
color: Colors.grey,
),
SizedBox(height: 20),
Text(
'Search for songs to add to the queue',
style: TextStyle(
color: Colors.grey,
fontSize: 16,
),
),
],
),
);
}
return ListView.builder(
padding: const EdgeInsets.all(20),
itemCount: _searchResults.length,
itemBuilder: (context, index) {
final track = _searchResults[index];
return _buildTrackCard(track);
},
);
}
Widget _buildQueueView(MusicQueueService queueService) {
final queue = queueService.queue;
if (queue.isEmpty) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.queue_music,
size: 80,
color: Colors.grey,
),
SizedBox(height: 20),
Text(
'Queue is empty',
style: TextStyle(
color: Colors.grey,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 10),
Text(
'Search and add songs to get started!',
style: TextStyle(
color: Colors.grey,
fontSize: 16,
),
),
],
),
);
}
return ListView.builder(
padding: const EdgeInsets.all(20),
itemCount: queue.length,
itemBuilder: (context, index) {
final queueItem = queue[index];
return _buildQueueItemCard(queueItem, index, queueService);
},
);
}
Widget _buildTrackCard(SpotifyTrack track) {
return Card(
color: const Color(0xFF1E1E1E),
margin: const EdgeInsets.only(bottom: 12),
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
// Album Art
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: const Color(0xFF2A2A2A),
),
child: track.album.images.isNotEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
track.album.images.first.url,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return const Icon(
Icons.music_note,
color: Colors.grey,
);
},
),
)
: const Icon(
Icons.music_note,
color: Colors.grey,
),
),
const SizedBox(width: 12),
// Queue List
// Track Info
Expanded(
child: ListView.builder(
itemCount: suggestedSongs.length,
itemBuilder: (context, index) {
final song = suggestedSongs[index];
return Container(
margin: const EdgeInsets.only(bottom: 10),
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: const Color(0xFF1E1E1E),
borderRadius: BorderRadius.circular(12),
border: song["userVoted"]
? Border.all(color: const Color(0xFF6366F1), width: 1)
: null,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
track.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
child: Row(
children: [
// Queue Position
Container(
width: 30,
height: 30,
decoration: BoxDecoration(
color: index < 3
? const Color(0xFF6366F1)
: const Color(0xFF6366F1).withOpacity(0.3),
shape: BoxShape.circle,
),
child: Center(
child: Text(
'${index + 1}',
style: TextStyle(
color: index < 3 ? Colors.white : Colors.grey,
fontWeight: FontWeight.bold,
fontSize: 12,
),
),
),
),
const SizedBox(width: 12),
// Album Art
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: const Color(0xFF6366F1).withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: song["imageUrl"] != null && song["imageUrl"].isNotEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
song["imageUrl"],
width: 50,
height: 50,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return const Icon(
Icons.music_note,
color: Color(0xFF6366F1),
size: 24,
);
},
),
)
: const Icon(
Icons.music_note,
color: Color(0xFF6366F1),
size: 24,
),
),
const SizedBox(width: 15),
// Song Info
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
song["title"],
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 16,
),
),
const SizedBox(height: 4),
Text(
"${song["artist"]}${song["duration"]}",
style: const TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
],
),
),
// Vote Buttons
Column(
children: [
// Upvote Button
GestureDetector(
onTap: () => _upvote(index),
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xFF22C55E).withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: const Icon(
Icons.keyboard_arrow_up,
color: Color(0xFF22C55E),
size: 20,
),
),
),
const SizedBox(height: 8),
// Vote Count
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: const Color(0xFF6366F1).withOpacity(0.2),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'${song["votes"]}',
style: const TextStyle(
color: Color(0xFF6366F1),
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
),
const SizedBox(height: 8),
// Downvote Button
GestureDetector(
onTap: () => _downvote(index),
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xFFEF4444).withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: const Icon(
Icons.keyboard_arrow_down,
color: Color(0xFFEF4444),
size: 20,
),
),
),
],
),
],
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
Text(
track.artists.map((a) => a.name).join(', '),
style: const TextStyle(
color: Colors.grey,
fontSize: 14,
),
);
},
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
Text(
track.album.name,
style: const TextStyle(
color: Colors.grey,
fontSize: 12,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
// Add Button
IconButton(
onPressed: () => _addToQueue(track),
icon: const Icon(
Icons.add_circle,
color: Color(0xFF6366F1),
size: 32,
),
),
],
@ -505,9 +372,120 @@ class _VotingPageState extends State<VotingPage> {
);
}
@override
void dispose() {
_searchController.dispose();
super.dispose();
Widget _buildQueueItemCard(QueueItem queueItem, int index, MusicQueueService queueService) {
return Card(
color: const Color(0xFF1E1E1E),
margin: const EdgeInsets.only(bottom: 12),
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
// Position
CircleAvatar(
backgroundColor: const Color(0xFF6366F1),
radius: 16,
child: Text(
'${index + 1}',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 12,
),
),
),
const SizedBox(width: 12),
// Album Art
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
color: const Color(0xFF2A2A2A),
),
child: queueItem.track.album.images.isNotEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(6),
child: Image.network(
queueItem.track.album.images.first.url,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return const Icon(
Icons.music_note,
color: Colors.grey,
size: 20,
);
},
),
)
: const Icon(
Icons.music_note,
color: Colors.grey,
size: 20,
),
),
const SizedBox(width: 12),
// Track Info
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
queueItem.track.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 14,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 2),
Text(
queueItem.track.artists.map((a) => a.name).join(', '),
style: const TextStyle(
color: Colors.grey,
fontSize: 12,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
// Voting Buttons
Column(
children: [
IconButton(
onPressed: () => queueService.upvote(index),
icon: const Icon(
Icons.keyboard_arrow_up,
color: Colors.green,
size: 28,
),
),
Text(
'${queueItem.votes}',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
IconButton(
onPressed: () => queueService.downvote(index),
icon: const Icon(
Icons.keyboard_arrow_down,
color: Colors.red,
size: 28,
),
),
],
),
],
),
),
);
}
}