visual demo version
This commit is contained in:
parent
1ce7aea6b5
commit
f70fe3cdd1
14 changed files with 2126 additions and 43 deletions
122
CHALLENGE_2/sleepysound/lib/models/spotify_track.dart
Normal file
122
CHALLENGE_2/sleepysound/lib/models/spotify_track.dart
Normal file
|
@ -0,0 +1,122 @@
|
|||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'spotify_track.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class SpotifyTrack {
|
||||
final String id;
|
||||
final String name;
|
||||
final List<SpotifyArtist> artists;
|
||||
final SpotifyAlbum album;
|
||||
@JsonKey(name: 'duration_ms')
|
||||
final int durationMs;
|
||||
@JsonKey(name: 'external_urls')
|
||||
final Map<String, String> externalUrls;
|
||||
@JsonKey(name: 'preview_url')
|
||||
final String? previewUrl;
|
||||
|
||||
SpotifyTrack({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.artists,
|
||||
required this.album,
|
||||
required this.durationMs,
|
||||
required this.externalUrls,
|
||||
this.previewUrl,
|
||||
});
|
||||
|
||||
factory SpotifyTrack.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpotifyTrackFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SpotifyTrackToJson(this);
|
||||
|
||||
String get artistNames => artists.map((artist) => artist.name).join(', ');
|
||||
|
||||
String get duration {
|
||||
final minutes = (durationMs / 60000).floor();
|
||||
final seconds = ((durationMs % 60000) / 1000).floor();
|
||||
return '$minutes:${seconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
String get imageUrl => album.images.isNotEmpty ? album.images.first.url : '';
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SpotifyArtist {
|
||||
final String id;
|
||||
final String name;
|
||||
|
||||
SpotifyArtist({
|
||||
required this.id,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
factory SpotifyArtist.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpotifyArtistFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SpotifyArtistToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SpotifyAlbum {
|
||||
final String id;
|
||||
final String name;
|
||||
final List<SpotifyImage> images;
|
||||
|
||||
SpotifyAlbum({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.images,
|
||||
});
|
||||
|
||||
factory SpotifyAlbum.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpotifyAlbumFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SpotifyAlbumToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SpotifyImage {
|
||||
final int height;
|
||||
final int width;
|
||||
final String url;
|
||||
|
||||
SpotifyImage({
|
||||
required this.height,
|
||||
required this.width,
|
||||
required this.url,
|
||||
});
|
||||
|
||||
factory SpotifyImage.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpotifyImageFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SpotifyImageToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SpotifySearchResponse {
|
||||
final SpotifyTracks tracks;
|
||||
|
||||
SpotifySearchResponse({required this.tracks});
|
||||
|
||||
factory SpotifySearchResponse.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpotifySearchResponseFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SpotifySearchResponseToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SpotifyTracks {
|
||||
final List<SpotifyTrack> items;
|
||||
final int total;
|
||||
|
||||
SpotifyTracks({
|
||||
required this.items,
|
||||
required this.total,
|
||||
});
|
||||
|
||||
factory SpotifyTracks.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpotifyTracksFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SpotifyTracksToJson(this);
|
||||
}
|
88
CHALLENGE_2/sleepysound/lib/models/spotify_track.g.dart
Normal file
88
CHALLENGE_2/sleepysound/lib/models/spotify_track.g.dart
Normal file
|
@ -0,0 +1,88 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'spotify_track.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
SpotifyTrack _$SpotifyTrackFromJson(Map<String, dynamic> json) => SpotifyTrack(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
artists:
|
||||
(json['artists'] as List<dynamic>)
|
||||
.map((e) => SpotifyArtist.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
album: SpotifyAlbum.fromJson(json['album'] as Map<String, dynamic>),
|
||||
durationMs: (json['duration_ms'] as num).toInt(),
|
||||
externalUrls: Map<String, String>.from(json['external_urls'] as Map),
|
||||
previewUrl: json['preview_url'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SpotifyTrackToJson(SpotifyTrack instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'name': instance.name,
|
||||
'artists': instance.artists,
|
||||
'album': instance.album,
|
||||
'duration_ms': instance.durationMs,
|
||||
'external_urls': instance.externalUrls,
|
||||
'preview_url': instance.previewUrl,
|
||||
};
|
||||
|
||||
SpotifyArtist _$SpotifyArtistFromJson(Map<String, dynamic> json) =>
|
||||
SpotifyArtist(id: json['id'] as String, name: json['name'] as String);
|
||||
|
||||
Map<String, dynamic> _$SpotifyArtistToJson(SpotifyArtist instance) =>
|
||||
<String, dynamic>{'id': instance.id, 'name': instance.name};
|
||||
|
||||
SpotifyAlbum _$SpotifyAlbumFromJson(Map<String, dynamic> json) => SpotifyAlbum(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
images:
|
||||
(json['images'] as List<dynamic>)
|
||||
.map((e) => SpotifyImage.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SpotifyAlbumToJson(SpotifyAlbum instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'name': instance.name,
|
||||
'images': instance.images,
|
||||
};
|
||||
|
||||
SpotifyImage _$SpotifyImageFromJson(Map<String, dynamic> json) => SpotifyImage(
|
||||
height: (json['height'] as num).toInt(),
|
||||
width: (json['width'] as num).toInt(),
|
||||
url: json['url'] as String,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SpotifyImageToJson(SpotifyImage instance) =>
|
||||
<String, dynamic>{
|
||||
'height': instance.height,
|
||||
'width': instance.width,
|
||||
'url': instance.url,
|
||||
};
|
||||
|
||||
SpotifySearchResponse _$SpotifySearchResponseFromJson(
|
||||
Map<String, dynamic> json,
|
||||
) => SpotifySearchResponse(
|
||||
tracks: SpotifyTracks.fromJson(json['tracks'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SpotifySearchResponseToJson(
|
||||
SpotifySearchResponse instance,
|
||||
) => <String, dynamic>{'tracks': instance.tracks};
|
||||
|
||||
SpotifyTracks _$SpotifyTracksFromJson(Map<String, dynamic> json) =>
|
||||
SpotifyTracks(
|
||||
items:
|
||||
(json['items'] as List<dynamic>)
|
||||
.map((e) => SpotifyTrack.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
total: (json['total'] as num).toInt(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SpotifyTracksToJson(SpotifyTracks instance) =>
|
||||
<String, dynamic>{'items': instance.items, 'total': instance.total};
|
|
@ -1,30 +1,317 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
class GroupPage extends StatelessWidget {
|
||||
class GroupPage extends StatefulWidget {
|
||||
const GroupPage({super.key});
|
||||
|
||||
@override
|
||||
State<GroupPage> createState() => _GroupPageState();
|
||||
}
|
||||
|
||||
class _GroupPageState extends State<GroupPage> {
|
||||
bool isConnectedToLido = true; // Simulate location verification
|
||||
String userName = "Guest #${DateTime.now().millisecond}";
|
||||
|
||||
List<Map<String, dynamic>> activeUsers = [
|
||||
{"name": "Alex M.", "joined": "2 min ago", "votes": 5, "isOnline": true},
|
||||
{"name": "Sarah K.", "joined": "5 min ago", "votes": 3, "isOnline": true},
|
||||
{"name": "Marco R.", "joined": "8 min ago", "votes": 7, "isOnline": true},
|
||||
{"name": "Lisa F.", "joined": "12 min ago", "votes": 2, "isOnline": false},
|
||||
{"name": "Tom B.", "joined": "15 min ago", "votes": 4, "isOnline": true},
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: const Color(0xFF121212),
|
||||
child: const Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.group, size: 100, color: Color(0xFF6366F1)),
|
||||
SizedBox(height: 20),
|
||||
Text(
|
||||
'Group',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
// Location Status Card
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: isConnectedToLido
|
||||
? const Color(0xFF22C55E).withOpacity(0.1)
|
||||
: const Color(0xFFEF4444).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
border: Border.all(
|
||||
color: isConnectedToLido
|
||||
? const Color(0xFF22C55E)
|
||||
: const Color(0xFFEF4444),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
isConnectedToLido ? Icons.location_on : Icons.location_off,
|
||||
size: 40,
|
||||
color: isConnectedToLido
|
||||
? const Color(0xFF22C55E)
|
||||
: const Color(0xFFEF4444),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(
|
||||
isConnectedToLido
|
||||
? '📍 Connected to Lido Schenna'
|
||||
: '❌ Not at Lido Schenna',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: isConnectedToLido
|
||||
? const Color(0xFF22C55E)
|
||||
: const Color(0xFFEF4444),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
Text(
|
||||
isConnectedToLido
|
||||
? 'You can now vote and suggest music!'
|
||||
: 'Please visit Lido Schenna to participate',
|
||||
style: const TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 14,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
Text(
|
||||
'Manage your listening group',
|
||||
style: TextStyle(color: Colors.grey),
|
||||
|
||||
const SizedBox(height: 25),
|
||||
|
||||
// User Info Section
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(18),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF1E1E1E),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
radius: 25,
|
||||
backgroundColor: const Color(0xFF6366F1),
|
||||
child: Text(
|
||||
userName.substring(0, 1),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 15),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
userName,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const Text(
|
||||
'Active since 5 minutes ago',
|
||||
style: TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF6366F1).withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.how_to_vote,
|
||||
color: Color(0xFF6366F1),
|
||||
size: 14,
|
||||
),
|
||||
SizedBox(width: 4),
|
||||
Text(
|
||||
'3 votes',
|
||||
style: TextStyle(
|
||||
color: Color(0xFF6366F1),
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 25),
|
||||
|
||||
// Active Users Section
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'Active Guests',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF6366F1).withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
'${activeUsers.where((user) => user["isOnline"]).length} online',
|
||||
style: const TextStyle(
|
||||
color: Color(0xFF6366F1),
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 15),
|
||||
|
||||
// Users List
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: activeUsers.length,
|
||||
itemBuilder: (context, index) {
|
||||
final user = activeUsers[index];
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 10),
|
||||
padding: const EdgeInsets.all(15),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF1E1E1E),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Stack(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
radius: 20,
|
||||
backgroundColor: const Color(0xFF6366F1),
|
||||
child: Text(
|
||||
user["name"].toString().substring(0, 1),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (user["isOnline"])
|
||||
Positioned(
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
child: Container(
|
||||
width: 12,
|
||||
height: 12,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF22C55E),
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: const Color(0xFF121212), width: 2),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(width: 15),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
user["name"],
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Joined ${user["joined"]}',
|
||||
style: const TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF6366F1).withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Text(
|
||||
'${user["votes"]} votes',
|
||||
style: const TextStyle(
|
||||
color: Color(0xFF6366F1),
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
// Bottom Action Buttons
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: isConnectedToLido ? () {
|
||||
// Refresh location/connection
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Location verified! ✓'),
|
||||
backgroundColor: Color(0xFF22C55E),
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
} : null,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Refresh Location'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF6366F1),
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -1,30 +1,273 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
class NowPlayingPage extends StatelessWidget {
|
||||
class NowPlayingPage extends StatefulWidget {
|
||||
const NowPlayingPage({super.key});
|
||||
|
||||
@override
|
||||
State<NowPlayingPage> createState() => _NowPlayingPageState();
|
||||
}
|
||||
|
||||
class _NowPlayingPageState extends State<NowPlayingPage> {
|
||||
// Mock data for demonstration
|
||||
bool isPlaying = false;
|
||||
String currentSong = "Summer Vibes";
|
||||
String currentArtist = "Chill Collective";
|
||||
double progress = 0.3; // 30% through song
|
||||
|
||||
List<Map<String, String>> upcomingQueue = [
|
||||
{"title": "Ocean Breeze", "artist": "Lofi Dreams", "votes": "12", "position": "1", "imageUrl": "https://i.scdn.co/image/ocean"},
|
||||
{"title": "Sunset Melody", "artist": "Acoustic Soul", "votes": "8", "position": "2", "imageUrl": "https://i.scdn.co/image/sunset"},
|
||||
{"title": "Peaceful Waters", "artist": "Nature Sounds", "votes": "5", "position": "3", "imageUrl": "https://i.scdn.co/image/water"},
|
||||
{"title": "Summer Nights", "artist": "Chill Vibes", "votes": "3", "position": "4", "imageUrl": "https://i.scdn.co/image/night"},
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: const Color(0xFF121212),
|
||||
child: const Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.music_note, size: 100, color: Color(0xFF6366F1)),
|
||||
SizedBox(height: 20),
|
||||
Text(
|
||||
'Now Playing',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
// Current Playing Section
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Album Art Placeholder
|
||||
Container(
|
||||
width: 200,
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF6366F1).withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all(color: const Color(0xFF6366F1), width: 2),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.music_note,
|
||||
size: 80,
|
||||
color: Color(0xFF6366F1),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// Song Info
|
||||
Text(
|
||||
currentSong,
|
||||
style: const TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
currentArtist,
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
color: Colors.grey,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// Progress Bar
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
child: Column(
|
||||
children: [
|
||||
LinearProgressIndicator(
|
||||
value: progress,
|
||||
backgroundColor: Colors.grey[800],
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF6366F1)),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
"${(progress * 3.5).toInt()}:${((progress * 3.5 % 1) * 60).toInt().toString().padLeft(2, '0')}",
|
||||
style: const TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
const Text(
|
||||
"3:30",
|
||||
style: TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
Text(
|
||||
'No music playing',
|
||||
style: TextStyle(color: Colors.grey),
|
||||
|
||||
// Queue Section
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
"Up Next",
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 8,
|
||||
height: 8,
|
||||
decoration: const BoxDecoration(
|
||||
color: Color(0xFF22C55E),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
const Text(
|
||||
"Live Queue",
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF22C55E),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: upcomingQueue.length,
|
||||
itemBuilder: (context, index) {
|
||||
final song = upcomingQueue[index];
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 10),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF1E1E1E),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// Queue Position
|
||||
Container(
|
||||
width: 24,
|
||||
height: 24,
|
||||
decoration: BoxDecoration(
|
||||
color: index < 2
|
||||
? const Color(0xFF6366F1)
|
||||
: const Color(0xFF6366F1).withOpacity(0.3),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${index + 1}',
|
||||
style: TextStyle(
|
||||
color: index < 2 ? Colors.white : Colors.grey,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 10,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
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: 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(
|
||||
song["title"]!,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
song["artist"]!,
|
||||
style: const TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF6366F1).withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.thumb_up,
|
||||
color: Color(0xFF6366F1),
|
||||
size: 12,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
song["votes"]!,
|
||||
style: const TextStyle(
|
||||
color: Color(0xFF6366F1),
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -1,34 +1,513 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import '../services/spotify_service.dart';
|
||||
import '../models/spotify_track.dart';
|
||||
|
||||
class VotingPage extends StatelessWidget {
|
||||
class VotingPage extends StatefulWidget {
|
||||
const VotingPage({super.key});
|
||||
|
||||
@override
|
||||
State<VotingPage> createState() => _VotingPageState();
|
||||
}
|
||||
|
||||
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 isSearching = false;
|
||||
|
||||
Future<void> _searchSpotify(String query) async {
|
||||
if (query.isEmpty) {
|
||||
setState(() {
|
||||
searchResults = [];
|
||||
isSearching = false;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
isSearching = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final tracks = await _spotifyService.searchTracks(query, limit: 10);
|
||||
setState(() {
|
||||
searchResults = tracks;
|
||||
isSearching = false;
|
||||
});
|
||||
} catch (e) {
|
||||
print('Error searching Spotify: $e');
|
||||
setState(() {
|
||||
searchResults = [];
|
||||
isSearching = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
// Show confirmation
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Added "${track.name}" to queue!'),
|
||||
backgroundColor: const Color(0xFF6366F1),
|
||||
duration: const Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
|
||||
// Clear search
|
||||
_searchController.clear();
|
||||
setState(() {
|
||||
searchResults = [];
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: const Color(0xFF121212),
|
||||
child: const Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.how_to_vote, size: 100, color: Color(0xFF6366F1)),
|
||||
SizedBox(height: 20),
|
||||
Text(
|
||||
'Voting',
|
||||
// Search Section
|
||||
const Text(
|
||||
'Suggest a Song',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
Text(
|
||||
'Vote for the next song',
|
||||
style: TextStyle(color: Colors.grey),
|
||||
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,
|
||||
children: [
|
||||
const Text(
|
||||
'Community Queue',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
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,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
|
||||
// Queue List
|
||||
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: 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,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
|
181
CHALLENGE_2/sleepysound/lib/services/spotify_service.dart
Normal file
181
CHALLENGE_2/sleepysound/lib/services/spotify_service.dart
Normal file
|
@ -0,0 +1,181 @@
|
|||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/spotify_track.dart';
|
||||
|
||||
class SpotifyService {
|
||||
static const String _clientId = 'YOUR_SPOTIFY_CLIENT_ID'; // You'll need to get this from Spotify Developer Console
|
||||
static const String _clientSecret = 'YOUR_SPOTIFY_CLIENT_SECRET'; // You'll need to get this from Spotify Developer Console
|
||||
static const String _baseUrl = 'https://api.spotify.com/v1';
|
||||
static const String _authUrl = 'https://accounts.spotify.com/api/token';
|
||||
|
||||
String? _accessToken;
|
||||
|
||||
// For demo purposes, we'll use Client Credentials flow (no user login required)
|
||||
// In a real app, you'd want to implement Authorization Code flow for user-specific features
|
||||
|
||||
Future<void> _getAccessToken() async {
|
||||
try {
|
||||
final response = await http.post(
|
||||
Uri.parse(_authUrl),
|
||||
headers: {
|
||||
'Authorization': 'Basic ${base64Encode(utf8.encode('$_clientId:$_clientSecret'))}',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: 'grant_type=client_credentials',
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
_accessToken = data['access_token'];
|
||||
|
||||
// Save token to shared preferences
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('spotify_access_token', _accessToken!);
|
||||
|
||||
print('Spotify access token obtained successfully');
|
||||
} else {
|
||||
print('Failed to get Spotify access token: ${response.statusCode}');
|
||||
print('Response body: ${response.body}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error getting Spotify access token: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _ensureValidToken() async {
|
||||
if (_accessToken == null) {
|
||||
// Try to load from shared preferences first
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_accessToken = prefs.getString('spotify_access_token');
|
||||
|
||||
if (_accessToken == null) {
|
||||
await _getAccessToken();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<SpotifyTrack>> searchTracks(String query, {int limit = 20}) async {
|
||||
try {
|
||||
await _ensureValidToken();
|
||||
|
||||
if (_accessToken == null) {
|
||||
// Return demo data if no token available
|
||||
return _getDemoTracks(query);
|
||||
}
|
||||
|
||||
final encodedQuery = Uri.encodeQueryComponent(query);
|
||||
final response = await http.get(
|
||||
Uri.parse('$_baseUrl/search?q=$encodedQuery&type=track&limit=$limit'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $_accessToken',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
final searchResponse = SpotifySearchResponse.fromJson(data);
|
||||
return searchResponse.tracks.items;
|
||||
} else if (response.statusCode == 401) {
|
||||
// Token expired, get a new one
|
||||
_accessToken = null;
|
||||
await _getAccessToken();
|
||||
return searchTracks(query, limit: limit); // Retry
|
||||
} else {
|
||||
print('Spotify search failed: ${response.statusCode}');
|
||||
print('Response body: ${response.body}');
|
||||
return _getDemoTracks(query);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error searching Spotify: $e');
|
||||
return _getDemoTracks(query);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<SpotifyTrack>> getPopularTracks({String genre = 'chill'}) async {
|
||||
try {
|
||||
await _ensureValidToken();
|
||||
|
||||
if (_accessToken == null) {
|
||||
return _getDemoPopularTracks();
|
||||
}
|
||||
|
||||
// Search for popular tracks in the genre
|
||||
final response = await http.get(
|
||||
Uri.parse('$_baseUrl/search?q=genre:$genre&type=track&limit=10'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $_accessToken',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
final searchResponse = SpotifySearchResponse.fromJson(data);
|
||||
return searchResponse.tracks.items;
|
||||
} else {
|
||||
return _getDemoPopularTracks();
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error getting popular tracks: $e');
|
||||
return _getDemoPopularTracks();
|
||||
}
|
||||
}
|
||||
|
||||
// Demo data for when Spotify API is not available
|
||||
List<SpotifyTrack> _getDemoTracks(String query) {
|
||||
final demoTracks = [
|
||||
_createDemoTrack('1', 'Tropical House Cruises', 'Kygo', 'Cloud Nine', 'https://i.scdn.co/image/tropical'),
|
||||
_createDemoTrack('2', 'Summer Breeze', 'Seeb', 'Summer Hits', 'https://i.scdn.co/image/summer'),
|
||||
_createDemoTrack('3', 'Relaxing Waves', 'Chillhop Music', 'Chill Collection', 'https://i.scdn.co/image/waves'),
|
||||
_createDemoTrack('4', 'Sunset Vibes', 'Odesza', 'In Return', 'https://i.scdn.co/image/sunset'),
|
||||
_createDemoTrack('5', 'Ocean Dreams', 'Emancipator', 'Soon It Will Be Cold Enough', 'https://i.scdn.co/image/ocean'),
|
||||
];
|
||||
|
||||
// Filter based on query
|
||||
if (query.toLowerCase().contains('tropical') || query.toLowerCase().contains('kygo')) {
|
||||
return [demoTracks[0]];
|
||||
} else if (query.toLowerCase().contains('summer')) {
|
||||
return [demoTracks[1]];
|
||||
} else if (query.toLowerCase().contains('chill') || query.toLowerCase().contains('relax')) {
|
||||
return [demoTracks[2], demoTracks[4]];
|
||||
} else if (query.toLowerCase().contains('sunset')) {
|
||||
return [demoTracks[3]];
|
||||
}
|
||||
|
||||
return demoTracks;
|
||||
}
|
||||
|
||||
List<SpotifyTrack> _getDemoPopularTracks() {
|
||||
return [
|
||||
_createDemoTrack('pop1', 'Ocean Breeze', 'Lofi Dreams', 'Summer Collection', 'https://i.scdn.co/image/ocean'),
|
||||
_createDemoTrack('pop2', 'Sunset Melody', 'Acoustic Soul', 'Peaceful Moments', 'https://i.scdn.co/image/sunset'),
|
||||
_createDemoTrack('pop3', 'Peaceful Waters', 'Nature Sounds', 'Tranquil Vibes', 'https://i.scdn.co/image/water'),
|
||||
_createDemoTrack('pop4', 'Summer Nights', 'Chill Vibes', 'Evening Sessions', 'https://i.scdn.co/image/night'),
|
||||
];
|
||||
}
|
||||
|
||||
SpotifyTrack _createDemoTrack(String id, String name, String artistName, String albumName, String imageUrl) {
|
||||
return SpotifyTrack(
|
||||
id: id,
|
||||
name: name,
|
||||
artists: [SpotifyArtist(id: 'artist_$id', name: artistName)],
|
||||
album: SpotifyAlbum(
|
||||
id: 'album_$id',
|
||||
name: albumName,
|
||||
images: [SpotifyImage(height: 640, width: 640, url: imageUrl)],
|
||||
),
|
||||
durationMs: 210000 + (id.hashCode % 120000), // Random duration between 3:30 and 5:30
|
||||
externalUrls: {'spotify': 'https://open.spotify.com/track/$id'},
|
||||
previewUrl: null,
|
||||
);
|
||||
}
|
||||
|
||||
// Method to initialize with your Spotify credentials
|
||||
static void setCredentials(String clientId, String clientSecret) {
|
||||
// In a real app, you'd store these securely
|
||||
// For demo purposes, we'll use the demo data
|
||||
print('Spotify credentials would be set here in a real app');
|
||||
print('Client ID: $clientId');
|
||||
print('For demo purposes, using mock data instead');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue