Home > Blockchain >  Perform two actions on the press of an IconButton
Perform two actions on the press of an IconButton

Time:03-15

I am working on a music player, and on pressing next button I want to perform two functions one is to add song to the queue and another function is used to trigger the plugin method of 'addSong'. On button pressed I wish to execute two functions which are '_pageManager.addSong' and 'addSongToQueue'.

This is the code

ValueListenableBuilder<bool>(
          valueListenable: _pageManager.isLastSongNotifier,
          builder: (_, isLast, __) {
            return IconButton(
              icon: Icon(Icons.skip_next),
              onPressed: (isLast)
                  ? _pageManager.addSong
                  : _pageManager.onNextSongButtonPressed;
            );
          }), 

CodePudding user response:

Don't use a ternary, and instead just use a function body.

onPressed: () {
  function1();
  function2();
}

CodePudding user response:

You can use if in the oppressed function like this

 onPressed: (){
  if(isLast){
    _pageManager.addSong
   }else{                                            
    _pageManager.onNextSongButtonPressed;
   }
 }

or use it like this if you have to check only one condition

 onPressed: () =>
  isLast ? _pageManager.addSong
   :                                           
 _pageManager.onNextSongButtonPressed;

                                       
  • Related