Skip to content

Commit d21fda4

Browse files
committed
💚 create Song Player Page 50%
1 parent 9c011d5 commit d21fda4

File tree

7 files changed

+121
-22
lines changed

7 files changed

+121
-22
lines changed

lib/core/constants/app_urls.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class AppUrls {
2-
static const fireStorage =
2+
static const coverFireStorage =
3+
'https://firebasestorage.googleapis.com/v0/b/spotify-flutter-4aa82.appspot.com/o/covers%2F';
4+
static const songFireStorage =
35
'https://firebasestorage.googleapis.com/v0/b/spotify-flutter-4aa82.appspot.com/o/covers%2F';
46
static const temp = 'Son-Tung-';
57
static const mediaAlt = 'alt=media';

lib/presentation/home/widgets/news_songs.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class NewsSongs extends StatelessWidget {
6666
image: DecorationImage(
6767
fit: BoxFit.cover,
6868
image: NetworkImage(
69-
'${AppUrls.fireStorage}${AppUrls.temp}${songs[index].idImg}.jpg?${AppUrls.mediaAlt}',
69+
'${AppUrls.coverFireStorage}${AppUrls.temp}${songs[index].idImg}.jpg?${AppUrls.mediaAlt}',
7070
),
7171
),
7272
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'package:hydrated_bloc/hydrated_bloc.dart';
2+
import 'package:just_audio/just_audio.dart';
3+
import 'package:spotify_with_flutter/presentation/song_player.dart/bloc/song_player_state.dart';
4+
5+
class SongPlayerCubit extends Cubit<SongPlayerState> {
6+
AudioPlayer audioPlayer = AudioPlayer();
7+
8+
Duration songDuration = Duration.zero;
9+
Duration songPosition = Duration.zero;
10+
11+
SongPlayerCubit() : super(SongPlayerLoading()) {
12+
audioPlayer.positionStream.listen((position) {
13+
songPosition = position;
14+
updateSongPlayer();
15+
});
16+
17+
audioPlayer.positionStream.listen((duration) {
18+
songDuration = duration;
19+
});
20+
}
21+
22+
void updateSongPlayer() {
23+
emit(
24+
SongPlayerLoaded(),
25+
);
26+
}
27+
28+
Future<void> loadSong(String url) async {
29+
try {
30+
await audioPlayer.setUrl(url);
31+
emit(
32+
SongPlayerLoaded(),
33+
);
34+
} catch (e) {
35+
emit(
36+
SongPlayerFailure(),
37+
);
38+
}
39+
}
40+
41+
void playOrPauseSong() {
42+
if (audioPlayer.playing) {
43+
audioPlayer.stop();
44+
} else {
45+
audioPlayer.play();
46+
}
47+
48+
emit(
49+
SongPlayerLoaded(),
50+
);
51+
}
52+
53+
@override
54+
Future<void> close() {
55+
audioPlayer.dispose();
56+
return super.close();
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import 'package:spotify_with_flutter/domain/entities/songs/songs.dart';
2-
31
abstract class SongPlayerState {}
42

53
class SongPlayerLoading extends SongPlayerState {}
64

7-
class SongPlayerLoaded extends SongPlayerState {
8-
final List<SongEntity> songs;
9-
SongPlayerLoaded({required this.songs});
10-
}
5+
class SongPlayerLoaded extends SongPlayerState {}
116

12-
class SongPlayerLoadFailure extends SongPlayerState {}
7+
class SongPlayerFailure extends SongPlayerState {}

lib/presentation/song_player.dart/pages/song_player.dart

+48-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_bloc/flutter_bloc.dart';
23
import 'package:spotify_with_flutter/common/helpers/is_dark_mode.dart';
34
import 'package:spotify_with_flutter/common/widgets/appbar/app_bar.dart';
45
import 'package:spotify_with_flutter/core/configs/theme/app_color.dart';
56
import 'package:spotify_with_flutter/core/constants/app_urls.dart';
67
import 'package:spotify_with_flutter/domain/entities/songs/songs.dart';
8+
import 'package:spotify_with_flutter/presentation/song_player.dart/bloc/song_player_cubit.dart';
9+
import 'package:spotify_with_flutter/presentation/song_player.dart/bloc/song_player_state.dart';
710

811
class SongPlayerPage extends StatelessWidget {
912
final SongEntity songEntity;
@@ -28,21 +31,27 @@ class SongPlayerPage extends StatelessWidget {
2831
onPressed: () {},
2932
icon: Icon(
3033
Icons.more_vert_rounded,
31-
color: context.isDarkMode
32-
? const Color(0xff959595)
33-
: const Color(0xff555555),
34+
color: context.isDarkMode ? const Color(0xff959595) : const Color(0xff555555),
3435
),
3536
),
3637
),
37-
body: SingleChildScrollView(
38-
child: Padding(
39-
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
40-
child: Column(
41-
children: [
42-
_songCover(context),
43-
const SizedBox(height: 10),
44-
_songDetail(),
45-
],
38+
body: BlocProvider(
39+
create: (_) => SongPlayerCubit()
40+
..loadSong(
41+
'${AppUrls.songFireStorage}${AppUrls.temp}${songEntity.idImg}.mp3?${AppUrls.mediaAlt}',
42+
),
43+
child: SingleChildScrollView(
44+
child: Padding(
45+
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
46+
child: Column(
47+
children: [
48+
_songCover(context),
49+
const SizedBox(height: 10),
50+
_songDetail(),
51+
const SizedBox(height: 30),
52+
_songPlayer(context),
53+
],
54+
),
4655
),
4756
),
4857
),
@@ -57,7 +66,7 @@ class SongPlayerPage extends StatelessWidget {
5766
image: DecorationImage(
5867
fit: BoxFit.cover,
5968
image: NetworkImage(
60-
'${AppUrls.fireStorage}${AppUrls.temp}${songEntity.idImg}.jpg?${AppUrls.mediaAlt}',
69+
'${AppUrls.coverFireStorage}${AppUrls.temp}${songEntity.idImg}.jpg?${AppUrls.mediaAlt}',
6170
),
6271
),
6372
),
@@ -101,4 +110,30 @@ class SongPlayerPage extends StatelessWidget {
101110
],
102111
);
103112
}
113+
114+
Widget _songPlayer(BuildContext context) {
115+
return BlocBuilder<SongPlayerCubit, SongPlayerState>(
116+
builder: (BuildContext context, SongPlayerState state) {
117+
if (state is SongPlayerLoading) {
118+
return const CircularProgressIndicator();
119+
}
120+
121+
if (state is SongPlayerLoaded) {
122+
return Column(
123+
children: [
124+
Slider(
125+
value: context.read<SongPlayerCubit>().songPosition.inSeconds.toDouble(),
126+
min: 0.0,
127+
max: context.read<SongPlayerCubit>().songPosition.inSeconds.toDouble(),
128+
onChanged: (value) {},
129+
),
130+
const SizedBox(height: 20),
131+
],
132+
);
133+
}
134+
135+
return Container();
136+
},
137+
);
138+
}
104139
}

pubspec.lock

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ packages:
121121
url: "https://pub.dev"
122122
source: hosted
123123
version: "0.10.1"
124+
diacritic:
125+
dependency: "direct main"
126+
description:
127+
name: diacritic
128+
sha256: "12981945ec38931748836cd76f2b38773118d0baef3c68404bdfde9566147876"
129+
url: "https://pub.dev"
130+
source: hosted
131+
version: "0.1.6"
124132
fake_async:
125133
dependency: transitive
126134
description:

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dependencies:
4545
dartz:
4646
cloud_firestore:
4747
just_audio:
48+
diacritic:
4849

4950

5051
dev_dependencies:

0 commit comments

Comments
 (0)