2019-12-03 12:23:41 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
2019-12-02 21:05:49 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
import '../backend.dart';
|
|
|
|
|
|
|
|
|
|
class PlayPauseButton extends StatefulWidget {
|
|
|
|
|
@override
|
|
|
|
|
_PlayPauseButtonState createState() => _PlayPauseButtonState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _PlayPauseButtonState extends State<PlayPauseButton>
|
|
|
|
|
with SingleTickerProviderStateMixin {
|
|
|
|
|
AnimationController playPauseAnimation;
|
|
|
|
|
Backend backend;
|
2019-12-03 12:23:41 +01:00
|
|
|
StreamSubscription<bool> playingSubscription;
|
2019-12-02 21:05:49 +01:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
|
|
playPauseAnimation =
|
|
|
|
|
AnimationController(vsync: this, duration: Duration(milliseconds: 300));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didChangeDependencies() {
|
|
|
|
|
super.didChangeDependencies();
|
|
|
|
|
|
|
|
|
|
backend = Backend.of(context);
|
2019-12-03 12:23:41 +01:00
|
|
|
|
|
|
|
|
if (playingSubscription != null) {
|
|
|
|
|
playingSubscription.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playingSubscription = backend.playing.listen((playing) =>
|
2019-12-02 21:05:49 +01:00
|
|
|
playing ? playPauseAnimation.forward() : playPauseAnimation.reverse());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return IconButton(
|
|
|
|
|
icon: AnimatedIcon(
|
|
|
|
|
icon: AnimatedIcons.play_pause,
|
|
|
|
|
progress: playPauseAnimation,
|
|
|
|
|
),
|
|
|
|
|
onPressed: backend.playPause,
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-03 12:23:41 +01:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
playingSubscription.cancel();
|
|
|
|
|
}
|
2019-12-02 21:05:49 +01:00
|
|
|
}
|