diff --git a/lib/app.dart b/lib/app.dart index bb11b5f..1757916 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'backend.dart'; @@ -14,6 +16,7 @@ class _AppState extends State with SingleTickerProviderStateMixin { AnimationController playerBarAnimation; Backend backend; + StreamSubscription playerActiveSubscription; @override void initState() { @@ -30,7 +33,12 @@ class _AppState extends State with SingleTickerProviderStateMixin { super.didChangeDependencies(); backend = Backend.of(context); - backend.playerActive.listen((active) => + + if (playerActiveSubscription != null) { + playerActiveSubscription.cancel(); + } + + playerActiveSubscription = backend.playerActive.listen((active) => active ? playerBarAnimation.forward() : playerBarAnimation.reverse()); } @@ -79,4 +87,10 @@ class _AppState extends State with SingleTickerProviderStateMixin { ), ); } + + @override + void dispose() { + super.dispose(); + playerActiveSubscription.cancel(); + } } diff --git a/lib/widgets/play_pause_button.dart b/lib/widgets/play_pause_button.dart index 42306f4..59af33a 100644 --- a/lib/widgets/play_pause_button.dart +++ b/lib/widgets/play_pause_button.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import '../backend.dart'; @@ -11,6 +13,7 @@ class _PlayPauseButtonState extends State with SingleTickerProviderStateMixin { AnimationController playPauseAnimation; Backend backend; + StreamSubscription playingSubscription; @override void initState() { @@ -25,7 +28,12 @@ class _PlayPauseButtonState extends State super.didChangeDependencies(); backend = Backend.of(context); - backend.playing.listen((playing) => + + if (playingSubscription != null) { + playingSubscription.cancel(); + } + + playingSubscription = backend.playing.listen((playing) => playing ? playPauseAnimation.forward() : playPauseAnimation.reverse()); } @@ -39,4 +47,10 @@ class _PlayPauseButtonState extends State onPressed: backend.playPause, ); } + + @override + void dispose() { + super.dispose(); + playingSubscription.cancel(); + } }