I am using WillPopScope and to navigate I am using Navigator.pushNamed(), so that onWillPop is not working fine, if I remove pushNamed and add pushAndRemoveUntil and make route false it works. But, I do not need like that I need to use pushNamed. How can I achieve this.
Try the below code :
onWillPop: () async {
Navigator.pushNamed();
return false;
},
Related
I need to make a change in my page (changing a bool in setState etc...) when the user presses the back button.
I looked it up and I know people use WillPopScope to override the default "back" action, but in all of the examples they were just popping the page, and I need to do a custom function instead of that, and have no Idea how to do so.
my function looks like this, and I need to run in when the user pressed the back button:
Future<void> backToCats() async{
setState(() {
isLoaded=false;
});
if(isFromSearch){
loadCategories();
}
setState(() {
showingBrands=false;
isLoaded=true;
});
}
You can perform custom logic in the onWillPop argument of a WillPopScope widget. Just make sure to return true or false at the end to indicate whether the page is allowed to pop. WillPopScope will activate for the back button, and other actions that automatically navigate back such as swiping from the side of the screen on iOS.
For example, if you need to set a bool to false in your stateful widget, it would look something like this
WillPopScope(
onWillPop: () async {
setState(() => myBool = false);
return true;
}
child: ...
);
Help! my will pop not even printing. Here is the code:
Future<bool> _willPopCallback() async {
print('Hi');
return false;
}
WillPopScope(
onWillPop: _willPopCallback,
child: Scaffold()
It was not working i tried my best so at the end i started using a package called back_button_interceptor. Its easy to use you can find here:
https://pub.dev/packages/back_button_interceptor/example
I have the following route:
StartWidget -> showModalBottomSheet -> NextWidget -> NextWidget: Doing some work and calls finally Navigator.pop and showing MainWidget but with open showModalBottomSheet.
Which navigator method do i have to use, in order to display only the StartWidget?
Thanks
Navigator.pushNamed returns a Future. As such, perhaps if you just add the await like so
void goToNextPage() async {
<...do something 1...>
await Navigator.pushNamed(context, screenName);
<...do something 2...> //open bottom sheet
}
It can solve the issue.
If I understood your question correctly, I think you can use the popUntil method to get back to the StartWidget, the documentation is here:
https://api.flutter.dev/flutter/widgets/Navigator/popUntil.html
I have a problem with a BottomNavigationBar. My app has two screens and one of them has a bottom sheet that doesn't hide on disposing of the first screen when I'm tapping on the icon of the second screen. BottomNavigationBar works normally and screens switch, but how to hide the bottom sheet of the first screen when shown the second screen I have no ideas. Could you help me solve that?
Calling:
void _showForm(int id) async {
showBottomSheet(
context: context,
elevation: 5,
builder: (context) => BottomSheetSwitch(_refreshJournals, id));
}
Closing (inside bottom sheet):
Navigator.of(context).pop();
P.S. I couldn't solve that problem, so I change showBottomSheet to showModalBottomSheet like that was in: how to set showModalBottomSheet to full height?
You can try to provide a key to the stateful widget of BottomSheet. And access that key's context just use the Navigator's pop method on tap or change of the screen. It might help you.
You can use it like
Navigator.of(key.currentContext).pop();
Else call the function of your BottomSheet using the key. Like as below
Declare GlobalKey with state in your currentWidget.
GloabalKey<BottomSheetWidgetState> closeKey = GloabalKey();
Declare one function closeBottomSheet in your bottomsheet widget state.
void closeBottomSheet(){ Navigator.of(conext).pop(); }
Call the function using the key.
closeKey.currentState.closeBottomSheet();
And done.
I want add data at all 'pop'
It two case,
Android back key and some button for 'navigator.(context).pop()'
So I used WillPopScope but I can't add data like this
onWillPop: () async {
return Navigator.of(context).pop(myData));
},
this is not working.
How can I add data?
If you call
Navigator.of(context).pop(myData)
in your code, the pop with arguments will be delivered if onWillPop return true.
When popping a page, onWillPop has the duty to day "ok you can pop" or "ok you cannot".