Newer
Older
Erik Hinkelmanns
committed
import 'dart:async';
Erik Hinkelmanns
committed
import 'package:ambient/widgets/MusicPlayerState.dart';
import 'package:flutter/material.dart';
Erik Hinkelmanns
committed
import 'package:flutter/services.dart';
import 'package:spotify_sdk/models/connection_status.dart';
import 'package:spotify_sdk/models/track.dart';
Erik Hinkelmanns
committed
import 'package:spotify_sdk/spotify_sdk.dart';
import 'dart:developer' as developer;
Erik Hinkelmanns
committed
import 'main.dart';
Erik Hinkelmanns
committed
void printErr(String msg) {
developer.log(msg);
}
Erik Hinkelmanns
committed
class HomePage extends StatelessWidget {
Erik Hinkelmanns
committed
final pageController;
const HomePage({super.key, this.pageController});
@override
Widget build(BuildContext context) {
Erik Hinkelmanns
committed
return StreamBuilder<ConnectionStatus>(
stream: SpotifySdk.subscribeConnectionStatus(),
builder: (context, snapshot) {
var data = snapshot.data;
if (data != null) {
MusicPlayerState
.of(context)
.connected = data.connected;
Erik Hinkelmanns
committed
}
return StreamBuilder<void>(
stream: MusicPlayerState.of(context).rebuildStream.stream,
Erik Hinkelmanns
committed
);
},
);
}
Widget player(BuildContext context) {
return Stack(children: [
padding: const EdgeInsets.all(8),
children: [
MusicPlayerState.of(context).connected
? _buildPlayerStateWidget(context)
child: MaterialButton(
minWidth: 200,
height: 200,
color: Colors.blue,
textColor: Colors.white,
shape: const CircleBorder(),
child: const Icon(
Icons.connected_tv,
size: 50,
),
onPressed: () async {
await MusicPlayerState.of(context).connectToSpotifyRemote();
IconButton(
icon: const Icon(Icons.info),
iconSize: 50,
color: primaryColor,
onPressed: () => checkIfAppIsActive(context),
),
MusicPlayerState.of(context).loading
? Container(
color: Colors.black12,
child: const Center(child: CircularProgressIndicator()))
Widget _buildPlayerStateWidget(BuildContext context) {
Track? track;
if(MusicPlayerState.of(context).playerState == null){
return const Text("PlayState is Null");
} else {
track = MusicPlayerState.of(context).playerState!.track;
}
if (MusicPlayerState.of(context).playerState?.track == null) {
return Center(
child: MaterialButton(
minWidth: 200,
height: 200,
color: Colors.blue,
textColor: Colors.white,
shape: const CircleBorder(),
onPressed: (){
Erik Hinkelmanns
committed
pageController.animateToPage(2,
duration: const Duration(milliseconds: 500),
curve: Curves.ease);
//_title = _titleList[2];
child: const Icon(
Icons.play_arrow_rounded,
size: 50,
),
),
);
} else {
MusicPlayerState.of(context).currentTrackImageUri = track!.imageUri;
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('''
Erik Hinkelmanns
committed
${track.name}
${track.artist.name}
${track.album.name}
''',
maxLines: 20,
style: const TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black)),
MusicPlayerState
.of(context)
.connected
? MusicPlayerState.of(context).spotifyImageWidget()
: const Text('Connect to see an image...'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
iconSize: 50.0,
icon: const Padding(
padding: EdgeInsets.zero,
child: Icon(Icons.skip_previous_outlined)),
onPressed: skipPrevious,
),
StreamBuilder<void>(
stream: MusicPlayerState.of(context).playStatedStream.stream,
builder: (context, _) => ControlButtons(),
),
IconButton(
iconSize: 50.0,
icon: const Padding(
padding: EdgeInsets.zero,
child: Icon(Icons.skip_next_outlined)),
onPressed: skipNext,
),
],
),
],
);
}
Erik Hinkelmanns
committed
}
Erik Hinkelmanns
committed
try {
await SpotifySdk.queue(
Erik Hinkelmanns
committed
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Erik Hinkelmanns
committed
try {
await SpotifySdk.play(spotifyUri: 'spotify:track:$songId');
Erik Hinkelmanns
committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Future<void> pause() async {
try {
await SpotifySdk.pause();
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Future<void> resume() async {
try {
await SpotifySdk.resume();
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Erik Hinkelmanns
committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Future<void> skipNext() async {
try {
await SpotifySdk.skipNext();
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Future<void> skipPrevious() async {
try {
await SpotifySdk.skipPrevious();
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Future<void> checkIfAppIsActive(BuildContext context) async {
try {
var isActive = await SpotifySdk.isSpotifyAppActive;
final snackBar = SnackBar(
content: Text(isActive
? 'Spotify app connection is active (currently playing)'
: 'Spotify app connection is not active (currently not playing)'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} on PlatformException catch (e) {
setStatus(e.code, message: e.message);
} on MissingPluginException {
setStatus('not implemented');
}
}
Erik Hinkelmanns
committed
void setStatus(String code, {String? message}) {
var text = message ?? '';
printErr(text);
}
void buildSnackbar(BuildContext context) {
final snackBar = SnackBar(
content: Text(MusicPlayerState
.of(context)
.connected
? 'connect to spotify successful'
: 'connect to spotify failed'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
class ControlButtons extends StatelessWidget {
const ControlButtons({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if (MusicPlayerState
.of(context)
.playerState == null) {
return const Text("PlayState is Null");
} else {
if (MusicPlayerState
.of(context)
.playerState!
.isPaused) {
return IconButton(
icon: const Icon(Icons.play_arrow_outlined),
iconSize: 50,
onPressed: () {
var state = MusicPlayerState.of(context);
SpotifySdk.resume()
.then((value) => state.updatePlayerState());
});
} else {
return IconButton(
icon: const Icon(Icons.pause_outlined),
iconSize: 50,
color: primaryColor,
onPressed: () {
var state = MusicPlayerState.of(context);
SpotifySdk.pause()
.then((value) => state.updatePlayerState());
},
);
}
}
}