From 8a64fc757780aeac04d2eaae1c362c1146d04785 Mon Sep 17 00:00:00 2001 From: Angshuman Barpujari Date: Sat, 4 Jul 2020 01:03:19 +0530 Subject: [PATCH] fixes some state issue, library song list load into appstate is still give issue warning --- lib/logic/models/playerStateNotify.dart | 2 +- lib/ux/components/cards/miniPlayer.dart | 61 +++++++++++-------- .../components/cards/playerAlbumArtCard.dart | 25 +------- .../components/groups/groupPlayerControl.dart | 59 +++++++++++------- .../groups/groupPlayerTextMeta.dart | 24 +------- .../components/lists/verticalListSimple.dart | 23 +------ lib/ux/pages/libraryPages.dart | 11 +--- lib/ux/pages/playerUIPage.dart | 33 +--------- 8 files changed, 83 insertions(+), 155 deletions(-) diff --git a/lib/logic/models/playerStateNotify.dart b/lib/logic/models/playerStateNotify.dart index 68fd11a..ebae3d6 100644 --- a/lib/logic/models/playerStateNotify.dart +++ b/lib/logic/models/playerStateNotify.dart @@ -9,7 +9,7 @@ class PlayerStateNotify with ChangeNotifier{ List get songinfos => _songinfos; - set songInfos(List songinfolist){ + set songinfos(List songinfolist){ _songinfos = songinfolist; notifyListeners(); } diff --git a/lib/ux/components/cards/miniPlayer.dart b/lib/ux/components/cards/miniPlayer.dart index d03f6fe..d2bdb1e 100644 --- a/lib/ux/components/cards/miniPlayer.dart +++ b/lib/ux/components/cards/miniPlayer.dart @@ -19,8 +19,6 @@ class MiniPlayer extends StatefulWidget { class _MiniPlayerState extends State with SingleTickerProviderStateMixin { AnimationController _controller; - PlayerStateNotify appstate; - PlaylistPosition appstatepos; StreamSubscription _streamSubscriptionState; bool isPlaying; PlayerLogic _playerLogic; @@ -38,39 +36,43 @@ class _MiniPlayerState extends State with SingleTickerProviderStateM @override void didChangeDependencies(){ super.didChangeDependencies(); - if(appstate==null) - appstate = Provider.of(context); - if(appstatepos==null) - appstatepos = Provider.of(context); _playerLogic.setMethodCallHandler(); _streamSubscriptionState = _playerLogic.onPlayerStateChanged.listen((state) { print("onStateChanged"); if(state == PlayerState.PLAYING){ - setState(() { - isPlaying = true; - }); - _controller.forward(); + if(mounted){ + setState(() { + isPlaying = true; + }); + _controller.forward(); + } } if(state == PlayerState.PAUSED){ - setState(() { - isPlaying = false; - }); - _controller.reverse(); + if(mounted){ + setState(() { + isPlaying = false; + }); + _controller.reverse(); + } } if(state == PlayerState.STOPPED){ - setState(() { - isPlaying = false; - }); - _controller.reverse(); + if(mounted){ + setState(() { + isPlaying = false; + }); + _controller.reverse(); + } } }); _playerLogic.onInstanceIsPlaying().then((isP) { - setState(() { - isPlaying = isP; - }); - if(isP){ - _controller.forward(from: _controller.upperBound); + if(mounted){ + setState(() { + isPlaying = isP; + }); + if(isP){ + _controller.forward(from: _controller.upperBound); + } } }); } @@ -94,6 +96,8 @@ class _MiniPlayerState extends State with SingleTickerProviderStateM @override Widget build(BuildContext context) { + var appstate = Provider.of(context); + var appstatepos = Provider.of(context); return Padding( padding: const EdgeInsets.fromLTRB(2,0,2,1), child: ClipRRect( @@ -148,7 +152,10 @@ class _MiniPlayerState extends State with SingleTickerProviderStateM icon: AnimatedIcons.play_pause, progress: _controller, ), - onPressed: ()=>onPlayClick() + onPressed: (){ + if(appstate.songinfos!=null&&appstatepos.index!=null) + onPlayClick(); + } ), ), ), @@ -162,8 +169,10 @@ class _MiniPlayerState extends State with SingleTickerProviderStateM color: Colors.white, icon: Icon(FlutterIcons.fast_forward_mdi), onPressed: (){ - _playerLogic.nextSong(appstate.songinfos, appstatepos.index); - ++appstatepos.index; + if(appstate.songinfos!=null&&appstatepos!=null){ + _playerLogic.nextSong(appstate.songinfos, appstatepos.index); + ++appstatepos.index; + } } ), ), diff --git a/lib/ux/components/cards/playerAlbumArtCard.dart b/lib/ux/components/cards/playerAlbumArtCard.dart index f493394..195e551 100644 --- a/lib/ux/components/cards/playerAlbumArtCard.dart +++ b/lib/ux/components/cards/playerAlbumArtCard.dart @@ -8,31 +8,8 @@ import 'package:Raylex/logic/playerLogic.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -class PlayerAlbumArtCard extends StatefulWidget { - @override - _PlayerAlbumArtCardState createState() => _PlayerAlbumArtCardState(); -} - -class _PlayerAlbumArtCardState extends State { - - StreamSubscription _playlistPositionSubscription; - - @override - void initState(){ - super.initState(); - } - @override - void didChangeDependencies(){ - super.didChangeDependencies(); - - } - - @override - void dispose(){ - super.dispose(); - _playlistPositionSubscription.cancel(); - } +class PlayerAlbumArtCard extends StatelessWidget { @override Widget build(BuildContext context) { diff --git a/lib/ux/components/groups/groupPlayerControl.dart b/lib/ux/components/groups/groupPlayerControl.dart index dc88d80..f7d27b8 100644 --- a/lib/ux/components/groups/groupPlayerControl.dart +++ b/lib/ux/components/groups/groupPlayerControl.dart @@ -23,24 +23,26 @@ class _GroupPlayerControlState extends State with TickerProv StreamSubscription _subscriptionAudioPositionChanged; StreamSubscription _subscriptionPlayerStateChanged; StreamSubscription _subscriptionAudioDurationChanged; - PlaylistPosition appstatepos; - PlayerStateNotify appstatelist; + //PlaylistPosition appstatepos; + //PlayerStateNotify appstatelist; @override void didChangeDependencies(){ super.didChangeDependencies(); print("player didchangeddependencies"); print("initial $_audioDuration"); - appstatepos = Provider.of(context); - appstatelist = Provider.of(context); + final appstatepos = Provider.of(context, listen: false); + final appstatelist = Provider.of(context, listen: false); _subscriptionAudioPositionChanged = widget._playerLogic.onAudioPositionChanged.listen((pos) { + if(mounted) setState(() { _playerSeekValue = pos; }); }); _subscriptionAudioDurationChanged = widget._playerLogic.onDurationChanged.listen((duration) { print(duration.inMilliseconds.toString()); + if(mounted) setState(() { if(duration != Duration.zero) _audioDuration = duration; @@ -49,36 +51,45 @@ class _GroupPlayerControlState extends State with TickerProv _subscriptionPlayerStateChanged = widget._playerLogic.onPlayerStateChanged.listen((state) { print("onStateChanged"); if(state == PlayerState.PLAYING){ - setState(() { - isPlaying = true; - }); - _animationController.forward(); + if(mounted){ + setState(() { + isPlaying = true; + }); + _animationController.forward(); + } } if(state == PlayerState.PAUSED){ - setState(() { - isPlaying = false; - }); - _animationController.reverse(); + if(mounted){ + setState(() { + isPlaying = false; + }); + _animationController.reverse(); + } } if(state == PlayerState.STOPPED){ - setState(() { - isPlaying = false; - }); - _animationController.reverse(); - print("song stopped at ${_playerSeekValue.inSeconds} of ${_audioDuration.inSeconds}"); - if(_playerSeekValue.inSeconds == _audioDuration.inSeconds){ - print("song ended"); - widget._playerLogic.nextSong(appstatelist.songinfos, appstatepos.index); + if(mounted){ + setState(() { + isPlaying = false; + }); + _animationController.reverse(); + print("song stopped at ${_playerSeekValue.inSeconds} of ${_audioDuration.inSeconds}"); + if(_playerSeekValue.inSeconds == _audioDuration.inSeconds){ + print("song ended"); + widget._playerLogic.nextSong(appstatelist.songinfos, appstatepos.index); + ++appstatepos.index; + } } } }); widget._playerLogic.onInstanceIsPlaying().then((isP) { - setState(() { - isPlaying = isP; + if(mounted){ + setState(() { + isPlaying = isP; + }); if(isP){ _animationController.forward(from: _animationController.upperBound); } - }); + } }); } @@ -115,6 +126,8 @@ class _GroupPlayerControlState extends State with TickerProv @override Widget build(BuildContext context) { + var appstatelist = Provider.of(context); + var appstatepos = Provider.of(context); return Column( children: [ Container( diff --git a/lib/ux/components/groups/groupPlayerTextMeta.dart b/lib/ux/components/groups/groupPlayerTextMeta.dart index 8e37a30..15acf09 100644 --- a/lib/ux/components/groups/groupPlayerTextMeta.dart +++ b/lib/ux/components/groups/groupPlayerTextMeta.dart @@ -8,30 +8,8 @@ import 'package:Raylex/ux/components/animations/marqueeWidget.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -class GroupPlayerTextMeta extends StatefulWidget { - @override - _GroupPlayerTextMetaState createState() => _GroupPlayerTextMetaState(); -} - -class _GroupPlayerTextMetaState extends State { - - - @override - void initState(){ - super.initState(); - } - - @override - void didChangeDependencies(){ - super.didChangeDependencies(); - - } - - @override - void dispose(){ - super.dispose(); - } +class GroupPlayerTextMeta extends StatelessWidget { @override Widget build(BuildContext context) { diff --git a/lib/ux/components/lists/verticalListSimple.dart b/lib/ux/components/lists/verticalListSimple.dart index fac0ae8..263ad0f 100644 --- a/lib/ux/components/lists/verticalListSimple.dart +++ b/lib/ux/components/lists/verticalListSimple.dart @@ -17,24 +17,11 @@ class VerticalListSimple extends StatefulWidget { } class _VerticalListSimpleState extends State { - int currentPlaying; - PlaylistPosition appstate; - @override - void initState(){ - super.initState(); - } - @override - void didChangeDependencies(){ - super.didChangeDependencies(); - if(appstate==null) - appstate = Provider.of(context); - } - @override - void dispose(){ - super.dispose(); - } + @override Widget build(BuildContext context) { + var appstate = Provider.of(context); + return Expanded( child: ListView.builder( itemCount: widget.songinfo.length, @@ -60,7 +47,6 @@ class _VerticalListSimpleState extends State { return Icon( Icons.bubble_chart, color: Colors.pink.shade300, - ); else return Text(""); @@ -68,9 +54,6 @@ class _VerticalListSimpleState extends State { onTap: (){ PlayerLogic().playMusic(widget.songinfo.elementAt(index).uri); appstate.index = index; - setState(() { - currentPlaying = index; - }); } ); }, diff --git a/lib/ux/pages/libraryPages.dart b/lib/ux/pages/libraryPages.dart index 0d4acca..cae743c 100644 --- a/lib/ux/pages/libraryPages.dart +++ b/lib/ux/pages/libraryPages.dart @@ -13,15 +13,10 @@ class LibraryPages extends StatefulWidget { } class _LibraryPagesState extends State { - PlayerStateNotify appstate; - @override - void didChangeDependencies(){ - super.didChangeDependencies(); - if(appstate==null) - appstate=Provider.of(context); - } + @override Widget build(BuildContext context) { + var appstate = Provider.of(context); return Column( children: [ LibraryAppBar(), @@ -30,7 +25,7 @@ class _LibraryPagesState extends State { builder: (context, snap){ if(snap.data!=null){ List list = snap.data; - appstate.songInfos = list; + appstate.songinfos = list; return list.isNotEmpty?VerticalListSimple(list):Expanded( child: Center( child: Text("No songs found"), diff --git a/lib/ux/pages/playerUIPage.dart b/lib/ux/pages/playerUIPage.dart index c77b001..912aa51 100644 --- a/lib/ux/pages/playerUIPage.dart +++ b/lib/ux/pages/playerUIPage.dart @@ -16,37 +16,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_icons/flutter_icons.dart'; import 'package:provider/provider.dart'; -class PlayerUIPage extends StatefulWidget{ + +class PlayerUIPage extends StatelessWidget{ final PlayerLogic _playerLogic; PlayerUIPage(this._playerLogic); - @override - _PlayerUIPageState createState() => _PlayerUIPageState(); -} - -class _PlayerUIPageState extends State { - PlayerStateNotify appstate; - PlaylistPosition appstatepos; - //bool miniplayer=true; - - - - @override - void initState(){ - super.initState(); - } - - @override - void didChangeDependencies(){ - super.didChangeDependencies(); - appstate = Provider.of(context); - appstatepos = Provider.of(context); - } - - @override - void dispose(){ - super.dispose(); - } - @override Widget build(BuildContext context) { return Container( @@ -64,7 +37,7 @@ class _PlayerUIPageState extends State { //song name and artist name here GroupPlayerTextMeta(), //basics controls like seek, play pause, next, previous - GroupPlayerControl(widget._playerLogic), + GroupPlayerControl(_playerLogic), //acco controls like mute speaker of headset set shuffle and repeats GroupPlayerAccControl(), ],