2020-06-23 19:23:12 +00:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-10-16 18:54:04 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
import 'package:freezer/settings.dart';
|
|
|
|
|
|
|
|
import '../api/player.dart';
|
|
|
|
import 'cached_image.dart';
|
|
|
|
import 'player_screen.dart';
|
|
|
|
|
|
|
|
class PlayerBar extends StatelessWidget {
|
|
|
|
double get progress {
|
|
|
|
if (AudioService.playbackState == null) return 0.0;
|
|
|
|
if (AudioService.currentMediaItem == null) return 0.0;
|
|
|
|
if (AudioService.currentMediaItem.duration.inSeconds == 0) return 0.0; //Division by 0
|
|
|
|
return AudioService.playbackState.currentPosition.inSeconds / AudioService.currentMediaItem.duration.inSeconds;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:28:45 +00:00
|
|
|
double iconSize = 28;
|
2020-10-14 19:09:16 +00:00
|
|
|
bool _gestureRegistered = false;
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-14 19:09:16 +00:00
|
|
|
return GestureDetector(
|
|
|
|
onHorizontalDragUpdate: (details) async {
|
|
|
|
if (_gestureRegistered) return;
|
|
|
|
final double sensitivity = 12.69;
|
|
|
|
//Right swipe
|
|
|
|
_gestureRegistered = true;
|
|
|
|
if (details.delta.dx > sensitivity) {
|
|
|
|
await AudioService.skipToPrevious();
|
|
|
|
}
|
|
|
|
//Left
|
|
|
|
if (details.delta.dx < -sensitivity) {
|
2020-10-15 18:37:36 +00:00
|
|
|
|
2020-10-14 19:09:16 +00:00
|
|
|
await AudioService.skipToNext();
|
|
|
|
}
|
|
|
|
_gestureRegistered = false;
|
|
|
|
return;
|
2020-06-23 19:23:12 +00:00
|
|
|
},
|
2020-10-14 19:09:16 +00:00
|
|
|
child: StreamBuilder(
|
|
|
|
stream: Stream.periodic(Duration(milliseconds: 250)),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
2020-10-19 19:28:45 +00:00
|
|
|
if (AudioService.currentMediaItem == null)
|
|
|
|
return Container(width: 0, height: 0,);
|
2020-10-14 19:09:16 +00:00
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
color: Theme.of(context).bottomAppBarColor,
|
|
|
|
child: ListTile(
|
2020-10-19 19:28:45 +00:00
|
|
|
dense: true,
|
|
|
|
contentPadding: EdgeInsets.symmetric(horizontal: 8.0),
|
2020-10-16 18:54:04 +00:00
|
|
|
onTap: () {
|
2020-10-19 19:28:45 +00:00
|
|
|
Navigator.of(context).push(MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) => PlayerScreen()));
|
|
|
|
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
|
|
|
|
systemNavigationBarColor: settings.themeData
|
|
|
|
.scaffoldBackgroundColor,
|
|
|
|
));
|
|
|
|
},
|
|
|
|
leading: CachedImage(
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
url: AudioService.currentMediaItem.extras['thumb'] ??
|
|
|
|
AudioService.currentMediaItem.artUri,
|
|
|
|
),
|
|
|
|
title: Text(
|
|
|
|
AudioService.currentMediaItem.displayTitle,
|
|
|
|
overflow: TextOverflow.clip,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
AudioService.currentMediaItem.displaySubtitle,
|
|
|
|
overflow: TextOverflow.clip,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
PrevNextButton(iconSize, prev: true, hidePrev: true,),
|
|
|
|
PlayPauseButton(iconSize),
|
|
|
|
PrevNextButton(iconSize)
|
|
|
|
],
|
|
|
|
)
|
2020-10-14 19:09:16 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
height: 3.0,
|
|
|
|
child: LinearProgressIndicator(
|
|
|
|
backgroundColor: Theme.of(context).primaryColor.withOpacity(0.1),
|
|
|
|
value: progress,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
2020-10-19 19:28:45 +00:00
|
|
|
}
|
2020-10-14 19:09:16 +00:00
|
|
|
),
|
2020-06-23 19:23:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PrevNextButton extends StatelessWidget {
|
|
|
|
|
|
|
|
final double size;
|
|
|
|
final bool prev;
|
|
|
|
final bool hidePrev;
|
|
|
|
int i;
|
|
|
|
PrevNextButton(this.size, {this.prev = false, this.hidePrev = false});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (!prev) {
|
|
|
|
if (playerHelper.queueIndex == (AudioService.queue??[]).length - 1) {
|
|
|
|
return IconButton(
|
|
|
|
icon: Icon(Icons.skip_next),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return IconButton(
|
|
|
|
icon: Icon(Icons.skip_next),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed: () => AudioService.skipToNext(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (prev) {
|
|
|
|
if (i == 0) {
|
|
|
|
if (hidePrev) {
|
|
|
|
return Container(height: 0, width: 0,);
|
|
|
|
}
|
|
|
|
return IconButton(
|
|
|
|
icon: Icon(Icons.skip_previous),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return IconButton(
|
|
|
|
icon: Icon(Icons.skip_previous),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed: () => AudioService.skipToPrevious(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlayPauseButton extends StatelessWidget {
|
|
|
|
|
|
|
|
final double size;
|
|
|
|
PlayPauseButton(this.size);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
2020-07-18 21:45:48 +00:00
|
|
|
return StreamBuilder(
|
|
|
|
stream: AudioService.playbackStateStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
//Playing
|
|
|
|
if (AudioService.playbackState?.playing??false) {
|
|
|
|
return IconButton(
|
|
|
|
iconSize: this.size,
|
|
|
|
icon: Icon(Icons.pause),
|
|
|
|
onPressed: () => AudioService.pause()
|
|
|
|
);
|
|
|
|
}
|
2020-06-23 19:23:12 +00:00
|
|
|
|
2020-07-18 21:45:48 +00:00
|
|
|
//Paused
|
2020-09-18 17:36:41 +00:00
|
|
|
if ((!(AudioService.playbackState?.playing??false) &&
|
2020-07-18 21:45:48 +00:00
|
|
|
AudioService.playbackState.processingState == AudioProcessingState.ready) ||
|
|
|
|
//None state (stopped)
|
|
|
|
AudioService.playbackState.processingState == AudioProcessingState.none) {
|
|
|
|
return IconButton(
|
|
|
|
iconSize: this.size,
|
|
|
|
icon: Icon(Icons.play_arrow),
|
|
|
|
onPressed: () => AudioService.play()
|
|
|
|
);
|
|
|
|
}
|
2020-06-23 19:23:12 +00:00
|
|
|
|
2020-07-18 21:45:48 +00:00
|
|
|
switch (AudioService.playbackState.processingState) {
|
|
|
|
//Stopped/Error
|
|
|
|
case AudioProcessingState.error:
|
|
|
|
case AudioProcessingState.none:
|
|
|
|
case AudioProcessingState.stopped:
|
|
|
|
return Container(width: this.size, height: this.size);
|
|
|
|
//Loading, connecting, rewinding...
|
|
|
|
default:
|
|
|
|
return Container(
|
|
|
|
width: this.size,
|
|
|
|
height: this.size,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|