Search code examples
flutterdartwidgetdart-pub

Is there a proper replacment for WillPopScope rather than PopScope?


I had made a chatting application but I am having problems in it's chatting screen because I am using emoji_picker_flutte package to show a emoji board,and I had implemented it such that whenever the emoji board was on the a bool varaible was true and vice varsa and if ther user uses back navigation gesture first the emoji board will disapper then if the user uses back navigation gesture again then the page will pop.

When using WillPopScope there was no issue, but recently with PopScope it dosen't, I have tried placing the whole code in onPopInvoked just as migration said with canPop set to false but it dosen't work. I also tried using the whole code into seprate function with the return type as bool and using thet function on canPop but it failed because canPop is running all the time while that page is on while using this way the emoji board dosen't even appear and because as the emoji button is pressed and the variable turns true it immediately becomes false while everything is working normally.

bool _willpop() {
    if (_showEmoji) {
      setState(() => _showEmoji = !_showEmoji);
      return false;
    } else {
      return true;
    }
  }


PopScope (
canPop = _willpop();
child:...
)

Other way

_willpop(bool p) {
    if (p) return;
    if (_showEmoji) {
      change();
      return false;
    } else {
      return true;
    }
  }

PopScope (
canPop: false,
onPopInvoked: (didpop) => _willpop(didpop),
child:...
)

both of the ways don't work can anyone suggest a better way for this


Solution

  • I found the solution, here is the solution and it works as I wanted

      bool _showEmoji = false;
    
      void change() => setState(() => _showEmoji = !_showEmoji);
    
      _willpop() {
       if (_showEmoji) change(); 
      }
    
      PopScope(
      canPop: !_showEmoji,
      onPopInvoked: (_) => _willpop(),
      child:...,
      ),