2023-07-29 02:17:26 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:freezer/settings.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2023-09-26 00:06:59 +00:00
|
|
|
import 'package:rxdart/rxdart.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
import '../api/player.dart';
|
|
|
|
import 'cached_image.dart';
|
|
|
|
|
2023-10-08 10:53:22 +00:00
|
|
|
class FancyScaffold extends StatefulWidget {
|
|
|
|
final Widget bottomPanel;
|
|
|
|
final double bottomPanelHeight;
|
|
|
|
final Widget expandedPanel;
|
2023-10-16 14:54:56 +00:00
|
|
|
final Widget? bottomNavigationBar;
|
|
|
|
final Widget? drawer;
|
|
|
|
final Widget? bodyDrawer;
|
2023-10-08 10:53:22 +00:00
|
|
|
final Widget body;
|
2023-10-12 22:09:37 +00:00
|
|
|
final void Function(AnimationStatus)? onAnimationStatusChange;
|
2023-10-08 10:53:22 +00:00
|
|
|
|
|
|
|
const FancyScaffold({
|
|
|
|
required this.bottomPanel,
|
|
|
|
required this.bottomPanelHeight,
|
|
|
|
required this.expandedPanel,
|
|
|
|
required this.body,
|
2023-10-12 22:09:37 +00:00
|
|
|
this.onAnimationStatusChange,
|
2023-10-16 14:54:56 +00:00
|
|
|
this.bottomNavigationBar,
|
|
|
|
this.bodyDrawer,
|
|
|
|
this.drawer,
|
2023-10-08 10:53:22 +00:00
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
static FancyScaffoldState? of(BuildContext context) =>
|
|
|
|
context.findAncestorStateOfType<FancyScaffoldState>();
|
|
|
|
|
2023-10-08 10:53:22 +00:00
|
|
|
@override
|
2023-10-12 22:09:37 +00:00
|
|
|
FancyScaffoldState createState() => FancyScaffoldState();
|
2023-10-08 10:53:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
class FancyScaffoldState extends State<FancyScaffold>
|
2023-10-08 10:53:22 +00:00
|
|
|
with TickerProviderStateMixin {
|
|
|
|
// goes from 0 to 1 (double)
|
|
|
|
// 0 = preview, 1 = expanded
|
2023-10-12 22:09:37 +00:00
|
|
|
late final AnimationController dragController;
|
|
|
|
final statusNotifier =
|
|
|
|
ValueNotifier<AnimationStatus>(AnimationStatus.dismissed);
|
2023-10-08 10:53:22 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2023-10-12 22:09:37 +00:00
|
|
|
dragController = AnimationController(
|
2023-10-08 10:53:22 +00:00
|
|
|
vsync: this, duration: const Duration(milliseconds: 500));
|
2023-10-12 22:09:37 +00:00
|
|
|
dragController.addStatusListener((status) => statusNotifier.value = status);
|
|
|
|
statusNotifier.addListener(
|
|
|
|
() => widget.onAnimationStatusChange?.call(statusNotifier.value));
|
2023-10-08 10:53:22 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2023-10-12 22:09:37 +00:00
|
|
|
dragController.dispose();
|
2023-10-08 10:53:22 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final systemPadding = MediaQuery.of(context).viewPadding;
|
2023-10-16 14:54:56 +00:00
|
|
|
final defaultBottomPadding =
|
|
|
|
(widget.bottomNavigationBar == null ? 0 : 80.0) + systemPadding.bottom;
|
2023-10-08 10:53:22 +00:00
|
|
|
final screenHeight = MediaQuery.of(context).size.height;
|
2023-10-12 22:09:37 +00:00
|
|
|
final sizeAnimation = Tween<double>(
|
2023-10-08 10:53:22 +00:00
|
|
|
begin: widget.bottomPanelHeight / MediaQuery.of(context).size.height,
|
|
|
|
end: 1.0,
|
2023-10-12 22:09:37 +00:00
|
|
|
).animate(dragController);
|
2023-10-08 10:53:22 +00:00
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () {
|
2023-10-12 22:09:37 +00:00
|
|
|
if (statusNotifier.value == AnimationStatus.completed ||
|
|
|
|
statusNotifier.value == AnimationStatus.reverse) {
|
|
|
|
dragController.fling(velocity: -1.0);
|
2023-10-08 10:53:22 +00:00
|
|
|
return Future.value(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Future.value(true);
|
|
|
|
},
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
Positioned.fill(
|
|
|
|
child: Scaffold(
|
2023-10-16 14:54:56 +00:00
|
|
|
body: widget.bodyDrawer != null
|
|
|
|
? Row(children: [
|
|
|
|
widget.bodyDrawer!,
|
|
|
|
Expanded(child: widget.body)
|
|
|
|
])
|
|
|
|
: widget.body,
|
|
|
|
drawer: widget.drawer,
|
2023-10-08 10:53:22 +00:00
|
|
|
bottomNavigationBar: Column(
|
2023-10-12 22:09:37 +00:00
|
|
|
mainAxisSize: MainAxisSize.min,
|
2023-10-08 10:53:22 +00:00
|
|
|
children: [
|
|
|
|
SizedBox(height: widget.bottomPanelHeight),
|
2023-10-16 14:54:56 +00:00
|
|
|
if (widget.bottomNavigationBar != null)
|
|
|
|
SizeTransition(
|
|
|
|
axisAlignment: -1.0,
|
|
|
|
sizeFactor:
|
|
|
|
Tween(begin: 1.0, end: 0.0).animate(sizeAnimation),
|
|
|
|
child: widget.bottomNavigationBar,
|
|
|
|
),
|
2023-10-08 10:53:22 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
child: AnimatedBuilder(
|
2023-10-12 22:09:37 +00:00
|
|
|
animation: sizeAnimation,
|
2023-10-08 10:53:22 +00:00
|
|
|
builder: (context, child) {
|
2023-10-12 22:09:37 +00:00
|
|
|
final x = 1.0 - sizeAnimation.value;
|
2023-10-08 10:53:22 +00:00
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
bottom: (defaultBottomPadding /*+ 8.0*/) * x,
|
|
|
|
//right: 8.0 * x,
|
|
|
|
//left: 8.0 * x,
|
|
|
|
),
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: ValueListenableBuilder(
|
2023-10-12 22:09:37 +00:00
|
|
|
valueListenable: statusNotifier,
|
2023-10-08 10:53:22 +00:00
|
|
|
builder: (context, state, child) {
|
|
|
|
return GestureDetector(
|
|
|
|
onVerticalDragEnd: _onVerticalDragEnd,
|
|
|
|
onVerticalDragUpdate: _onVerticalDragUpdate,
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: SizeTransition(
|
2023-10-12 22:09:37 +00:00
|
|
|
sizeFactor: sizeAnimation,
|
2023-10-08 10:53:22 +00:00
|
|
|
axisAlignment: -1.0,
|
|
|
|
axis: Axis.vertical,
|
|
|
|
child: SizedBox(
|
|
|
|
height: screenHeight,
|
|
|
|
width: MediaQuery.of(context).size.width,
|
|
|
|
child: ValueListenableBuilder(
|
2023-10-12 22:09:37 +00:00
|
|
|
valueListenable: statusNotifier,
|
2023-10-08 10:53:22 +00:00
|
|
|
builder: (context, state, _) => Stack(
|
|
|
|
children: [
|
|
|
|
if (state != AnimationStatus.dismissed)
|
|
|
|
Positioned.fill(
|
2023-10-12 22:09:37 +00:00
|
|
|
key: const Key('player_screen'),
|
2023-10-08 10:53:22 +00:00
|
|
|
child: widget.expandedPanel,
|
|
|
|
),
|
|
|
|
if (state != AnimationStatus.completed)
|
|
|
|
Positioned(
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
left: 0,
|
2023-10-12 22:09:37 +00:00
|
|
|
key: const Key('player_bar'),
|
2023-10-08 10:53:22 +00:00
|
|
|
child: FadeTransition(
|
|
|
|
opacity: Tween(begin: 1.0, end: 0.0)
|
2023-10-12 22:09:37 +00:00
|
|
|
.animate(dragController),
|
2023-10-08 10:53:22 +00:00
|
|
|
child: SizedBox(
|
|
|
|
height: widget.bottomPanelHeight,
|
|
|
|
child: widget.bottomPanel),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onVerticalDragUpdate(DragUpdateDetails details) {
|
2023-10-12 22:09:37 +00:00
|
|
|
dragController.value -=
|
2023-10-08 10:53:22 +00:00
|
|
|
details.delta.dy / MediaQuery.of(context).size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onVerticalDragEnd(DragEndDetails details) {
|
|
|
|
// snap widget to size
|
|
|
|
// this should be also handled by drag velocity and not only with bare size.
|
|
|
|
|
|
|
|
const double minFlingVelocity = 365.0;
|
|
|
|
|
|
|
|
if (details.velocity.pixelsPerSecond.dy.abs() > minFlingVelocity) {
|
2023-10-12 22:09:37 +00:00
|
|
|
dragController.fling(
|
2023-10-08 10:53:22 +00:00
|
|
|
velocity: -details.velocity.pixelsPerSecond.dy /
|
|
|
|
MediaQuery.of(context).size.height);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
dragController.fling(velocity: dragController.value > 0.5 ? 1.0 : -1.0);
|
2023-10-08 10:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
class PlayerBar extends StatefulWidget {
|
2023-10-12 22:09:37 +00:00
|
|
|
final VoidCallback? onTap;
|
2023-07-29 02:17:26 +00:00
|
|
|
final bool shouldHaveHero;
|
|
|
|
final Color? backgroundColor;
|
2023-10-12 22:09:37 +00:00
|
|
|
final FocusNode? focusNode;
|
2023-07-29 02:17:26 +00:00
|
|
|
const PlayerBar({
|
|
|
|
Key? key,
|
2023-10-12 22:09:37 +00:00
|
|
|
this.onTap,
|
2023-07-29 02:17:26 +00:00
|
|
|
this.shouldHaveHero = true,
|
|
|
|
this.backgroundColor,
|
2023-10-12 22:09:37 +00:00
|
|
|
this.focusNode,
|
2023-07-29 02:17:26 +00:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
2023-10-12 22:09:37 +00:00
|
|
|
State<PlayerBar> createState() => _PlayerBarState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _PlayerBarState extends State<PlayerBar> {
|
|
|
|
final double iconSize = 28;
|
|
|
|
late StreamSubscription mediaItemSub;
|
|
|
|
late bool _isNothingPlaying = audioHandler.mediaItem.value == null;
|
|
|
|
|
|
|
|
double parsePosition(Duration position) {
|
|
|
|
if (audioHandler.mediaItem.value == null) return 0.0;
|
2023-10-12 22:09:37 +00:00
|
|
|
if (audioHandler.mediaItem.value!.duration!.inSeconds == 0) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return 0.0; //Division by 0
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
return position.inSeconds /
|
|
|
|
audioHandler.mediaItem.value!.duration!.inSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
mediaItemSub = audioHandler.mediaItem.listen((playingItem) {
|
|
|
|
if ((playingItem == null && !_isNothingPlaying) ||
|
2023-10-12 22:09:37 +00:00
|
|
|
(playingItem != null && _isNothingPlaying)) {
|
2023-07-29 02:17:26 +00:00
|
|
|
setState(() => _isNothingPlaying = playingItem == null);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
});
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
Color get backgroundColor =>
|
|
|
|
widget.backgroundColor ??
|
|
|
|
Theme.of(context).navigationBarTheme.backgroundColor ??
|
|
|
|
Theme.of(context).colorScheme.surface;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
mediaItemSub.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _gestureRegistered = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-10-08 10:53:22 +00:00
|
|
|
return SizedBox(
|
|
|
|
height: 68.0,
|
|
|
|
child: _isNothingPlaying
|
|
|
|
? null
|
|
|
|
: GestureDetector(
|
|
|
|
onHorizontalDragUpdate: (details) async {
|
|
|
|
if (_gestureRegistered) return;
|
2023-10-12 22:09:37 +00:00
|
|
|
const double sensitivity = 12.69;
|
2023-10-08 10:53:22 +00:00
|
|
|
//Right swipe
|
|
|
|
_gestureRegistered = true;
|
|
|
|
if (details.delta.dx > sensitivity) {
|
|
|
|
await audioHandler.skipToPrevious();
|
|
|
|
}
|
|
|
|
//Left
|
|
|
|
if (details.delta.dx < -sensitivity) {
|
|
|
|
await audioHandler.skipToNext();
|
|
|
|
}
|
|
|
|
_gestureRegistered = false;
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
|
|
|
|
Expanded(
|
|
|
|
child: StreamBuilder<MediaItem?>(
|
|
|
|
stream: audioHandler.mediaItem,
|
|
|
|
initialData: audioHandler.mediaItem.valueOrNull,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData) return const SizedBox();
|
|
|
|
final currentMediaItem = snapshot.data!;
|
|
|
|
final image = CachedImage(
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
url: currentMediaItem.extras!['thumb'] ??
|
|
|
|
currentMediaItem.artUri.toString(),
|
|
|
|
);
|
|
|
|
final leadingWidget = widget.shouldHaveHero
|
|
|
|
? Hero(tag: currentMediaItem.id, child: image)
|
|
|
|
: image;
|
|
|
|
return Material(
|
|
|
|
child: ListTile(
|
|
|
|
dense: true,
|
2023-10-12 22:09:37 +00:00
|
|
|
focusNode: widget.focusNode,
|
2023-10-08 10:53:22 +00:00
|
|
|
contentPadding:
|
2023-10-12 22:09:37 +00:00
|
|
|
const EdgeInsets.symmetric(horizontal: 8.0),
|
|
|
|
onTap: widget.onTap,
|
2023-10-08 10:53:22 +00:00
|
|
|
leading: AnimatedSwitcher(
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
child: leadingWidget),
|
|
|
|
title: Text(
|
|
|
|
currentMediaItem.displayTitle!,
|
|
|
|
overflow: TextOverflow.clip,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
currentMediaItem.displaySubtitle ?? '',
|
|
|
|
overflow: TextOverflow.clip,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
trailing: IconTheme(
|
|
|
|
data: IconThemeData(
|
|
|
|
color: settings.isDark
|
|
|
|
? Colors.white
|
|
|
|
: Colors.grey[600]),
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
PrevNextButton(
|
|
|
|
iconSize,
|
|
|
|
prev: true,
|
|
|
|
),
|
|
|
|
PlayPauseButton(iconSize),
|
|
|
|
PrevNextButton(iconSize)
|
|
|
|
],
|
2023-07-29 02:17:26 +00:00
|
|
|
),
|
2023-10-08 10:53:22 +00:00
|
|
|
)));
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 3.0,
|
|
|
|
child: StreamBuilder<Duration>(
|
|
|
|
stream: AudioService.position,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return LinearProgressIndicator(
|
|
|
|
value: parsePosition(snapshot.data ?? Duration.zero),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
);
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PrevNextButton extends StatelessWidget {
|
|
|
|
final double size;
|
|
|
|
final bool prev;
|
|
|
|
final bool hidePrev;
|
2023-10-12 22:09:37 +00:00
|
|
|
const PrevNextButton(this.size,
|
|
|
|
{super.key, this.prev = false, this.hidePrev = false});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-09-26 00:06:59 +00:00
|
|
|
// Listens to both mediaItem updates (a.k.a. when the song changes)
|
|
|
|
// and queue updates (so for example, in SmartTrackLists, when the songs are fetched, it's updated with the new items)
|
|
|
|
return StreamBuilder<bool>(
|
|
|
|
stream: Rx.combineLatest2<MediaItem?, List<MediaItem>, bool>(
|
|
|
|
audioHandler.mediaItem,
|
|
|
|
audioHandler.queue,
|
|
|
|
(_, queue) => playerHelper.queueIndex < queue.length - 1),
|
2023-07-29 02:17:26 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!prev) {
|
|
|
|
return IconButton(
|
2023-10-12 22:09:37 +00:00
|
|
|
splashRadius: size * 2.0,
|
2023-07-29 02:17:26 +00:00
|
|
|
icon: Icon(
|
|
|
|
Icons.skip_next,
|
|
|
|
semanticLabel: "Play next".i18n,
|
|
|
|
),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed:
|
2023-09-26 00:06:59 +00:00
|
|
|
snapshot.data == true ? () => audioHandler.skipToNext() : null,
|
2023-07-29 02:17:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
final canGoPrev = playerHelper.queueIndex > 0;
|
|
|
|
|
2023-09-26 00:06:59 +00:00
|
|
|
if (!canGoPrev && hidePrev) return const SizedBox.shrink();
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
return IconButton(
|
2023-10-12 22:09:37 +00:00
|
|
|
splashRadius: size * 2.0,
|
2023-07-29 02:17:26 +00:00
|
|
|
icon: Icon(
|
|
|
|
Icons.skip_previous,
|
|
|
|
semanticLabel: "Play previous".i18n,
|
|
|
|
),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed: canGoPrev ? () => audioHandler.skipToPrevious() : null,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PlayPauseButton extends StatefulWidget {
|
|
|
|
final double size;
|
|
|
|
final bool filled;
|
|
|
|
final Color? iconColor;
|
|
|
|
|
|
|
|
/// The color of the card if [filled] is true
|
|
|
|
final Color? color;
|
|
|
|
const PlayPauseButton(
|
|
|
|
this.size, {
|
|
|
|
Key? key,
|
|
|
|
this.filled = false,
|
|
|
|
this.color,
|
|
|
|
this.iconColor,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
2023-10-12 22:09:37 +00:00
|
|
|
State<PlayPauseButton> createState() => _PlayPauseButtonState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _PlayPauseButtonState extends State<PlayPauseButton>
|
|
|
|
with SingleTickerProviderStateMixin {
|
2023-10-14 14:26:13 +00:00
|
|
|
late final AnimationController _controller;
|
|
|
|
late final Animation<double> _animation;
|
2023-07-29 02:17:26 +00:00
|
|
|
late StreamSubscription _subscription;
|
2023-10-15 14:48:55 +00:00
|
|
|
late bool _canPlay = audioHandler.playbackState.value.processingState ==
|
|
|
|
AudioProcessingState.ready ||
|
2023-07-29 02:17:26 +00:00
|
|
|
audioHandler.playbackState.value.processingState ==
|
2023-10-15 14:48:55 +00:00
|
|
|
AudioProcessingState.idle;
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2023-10-14 14:26:13 +00:00
|
|
|
_controller = AnimationController(
|
2023-10-15 14:48:55 +00:00
|
|
|
vsync: this, duration: const Duration(milliseconds: 200));
|
2023-10-14 14:26:13 +00:00
|
|
|
_animation = CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
|
2023-10-15 14:48:55 +00:00
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
_subscription = audioHandler.playbackState.listen((playbackState) {
|
2023-10-15 14:48:55 +00:00
|
|
|
if (playbackState.processingState == AudioProcessingState.ready ||
|
|
|
|
audioHandler.playbackState.value.processingState ==
|
|
|
|
AudioProcessingState.idle) {
|
2023-10-12 22:09:37 +00:00
|
|
|
if (playbackState.playing) {
|
2023-07-29 02:17:26 +00:00
|
|
|
_controller.forward();
|
2023-10-12 22:09:37 +00:00
|
|
|
} else {
|
2023-07-29 02:17:26 +00:00
|
|
|
_controller.reverse();
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
if (!_canPlay) setState(() => _canPlay = true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() => _canPlay = false);
|
|
|
|
});
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_subscription.cancel();
|
|
|
|
_controller.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _playPause() {
|
2023-10-12 22:09:37 +00:00
|
|
|
if (audioHandler.playbackState.value.playing) {
|
2023-07-29 02:17:26 +00:00
|
|
|
audioHandler.pause();
|
2023-10-12 22:09:37 +00:00
|
|
|
} else {
|
2023-07-29 02:17:26 +00:00
|
|
|
audioHandler.play();
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final Widget? child;
|
|
|
|
if (_canPlay) {
|
|
|
|
final icon = AnimatedIcon(
|
|
|
|
icon: AnimatedIcons.play_pause,
|
|
|
|
progress: _animation,
|
|
|
|
semanticLabel: audioHandler.playbackState.value.playing
|
|
|
|
? 'Pause'.i18n
|
|
|
|
: 'Play'.i18n,
|
|
|
|
);
|
2023-10-12 22:09:37 +00:00
|
|
|
if (!widget.filled) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return IconButton(
|
|
|
|
color: widget.iconColor,
|
|
|
|
icon: icon,
|
|
|
|
iconSize: widget.size,
|
|
|
|
onPressed: _playPause);
|
2023-10-12 22:09:37 +00:00
|
|
|
} else {
|
|
|
|
child = Material(
|
|
|
|
type: MaterialType.transparency,
|
|
|
|
child: InkWell(
|
|
|
|
customBorder: const CircleBorder(),
|
|
|
|
onTap: _playPause,
|
|
|
|
child: IconTheme.merge(
|
|
|
|
child: Center(child: icon),
|
|
|
|
data: IconThemeData(
|
|
|
|
size: widget.size / 2, color: widget.iconColor))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-29 02:17:26 +00:00
|
|
|
switch (audioHandler.playbackState.value.processingState) {
|
|
|
|
//Stopped/Error
|
|
|
|
case AudioProcessingState.error:
|
|
|
|
child = null;
|
|
|
|
break;
|
|
|
|
//Loading, connecting, rewinding...
|
|
|
|
default:
|
|
|
|
child = const Center(child: CircularProgressIndicator());
|
|
|
|
break;
|
|
|
|
}
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
|
|
|
if (widget.filled) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return SizedBox.square(
|
|
|
|
dimension: widget.size,
|
2023-10-12 22:09:37 +00:00
|
|
|
child: AnimatedContainer(
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
decoration:
|
|
|
|
BoxDecoration(shape: BoxShape.circle, color: widget.color),
|
|
|
|
child: child));
|
|
|
|
} else {
|
2023-07-29 02:17:26 +00:00
|
|
|
return SizedBox.square(dimension: widget.size, child: child);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|