| 
									
										
										
										
											2019-12-03 12:23:41 +01:00
										 |  |  | import 'dart:async'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 21:05:49 +01:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							| 
									
										
										
										
											2020-05-04 21:49:44 +02:00
										 |  |  | import 'package:musicus_common/musicus_common.dart'; | 
					
						
							| 
									
										
										
										
											2019-12-02 21:05:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class PlayPauseButton extends StatefulWidget { | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   _PlayPauseButtonState createState() => _PlayPauseButtonState(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _PlayPauseButtonState extends State<PlayPauseButton> | 
					
						
							|  |  |  |     with SingleTickerProviderStateMixin { | 
					
						
							|  |  |  |   AnimationController playPauseAnimation; | 
					
						
							| 
									
										
										
										
											2020-05-04 21:49:44 +02:00
										 |  |  |   MusicusBackendState 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 12:33:43 +01:00
										 |  |  |     playPauseAnimation = AnimationController( | 
					
						
							|  |  |  |       vsync: this, | 
					
						
							|  |  |  |       duration: Duration(milliseconds: 300), | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-12-02 21:05:49 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   void didChangeDependencies() { | 
					
						
							|  |  |  |     super.didChangeDependencies(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-04 21:49:44 +02:00
										 |  |  |     backend = MusicusBackend.of(context); | 
					
						
							|  |  |  |     playPauseAnimation.value = backend.playback.playing.value ? 1.0 : 0.0; | 
					
						
							| 
									
										
										
										
											2019-12-03 12:23:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (playingSubscription != null) { | 
					
						
							|  |  |  |       playingSubscription.cancel(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-04 21:49:44 +02:00
										 |  |  |     playingSubscription = backend.playback.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, | 
					
						
							|  |  |  |       ), | 
					
						
							| 
									
										
										
										
											2020-05-04 21:49:44 +02:00
										 |  |  |       onPressed: backend.playback.playPause, | 
					
						
							| 
									
										
										
										
											2019-12-02 21:05:49 +01:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-12-03 12:23:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   void dispose() { | 
					
						
							|  |  |  |     super.dispose(); | 
					
						
							|  |  |  |     playingSubscription.cancel(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-12-02 21:05:49 +01:00
										 |  |  | } |