I want to make a custom back button that not only respect Navigation.canPop
but also PopScope.canPop
. I tried this logic but doesn’t work.
I tried to read the PopScope
source code and it uses ModalRoute
under the hood and adds _popEntries
accordingly. Thus, I presume ModalRoute.of(context).canPop()
should give that information, but it is not.
final isDissmissable = Navigation.of(context).canPop() &&
ModalRoute.of(context).canPop(); // this is wrong
What should I use to check if PopScope
is false
?
Never mind, I found the solution. I was using the wrong API. It should be popDisposition
instead of canPop()
.
ModalRoute.of(context).popDisposition
will give me the state of the pop entries. So my code is now like below.
final isDissmissable = Navigation.of(context).canPop() &&
ModalRoute.of(context).popDisposition != RoutePopDisposition.doNotPop;
As it is written in the documentation, popDisposition
will returns RoutePopDisposition.doNotPop
if there is pop entry found with canPop
false on the current route.