Back button flutter - flutter

i would need to switch a color between one screen and another.
Through Navigato.pop (context, color) I can make everything work but when I press the back button of the smartphone the color is not passed
Come to solve?

If you are trying to pass data via BackButton, then you can use WillpopScope. Wrap it in your top of widget's tree.
WillPopScope(
onWillPop: () async {
Navigator.pop(context, color);
return false;
},);

Related

How can I change or override android/ios back button's action in flutter

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: ...
);

How to go back in the previous page, upon tapping the phone's back button without showing any dialog?

// I already tried this but it wont work
return WillPopScope(
onWillPop: () { Navigator.pop(context);
},
child: Scaffold(),
error:Error: A non-null value must be returned since the return type 'Future' doesn't allow null.
'Future' is from 'dart:async'.
onWillPop: () { Navigator.pop(context);
If there is a screen in the stack then tapping the android back bottom will automatically go to the previous screen. this is the android default behavior. you don't have to wrap with WillPopScope.
If you want to show some dialog or do something by tapping the back button then wrap with WillPopScope.
onWillpop is an async function, it requires a function with a return type Future<bool>.
If you want to disable the default behavior of the android back button then you can use this.
onWillPop: () async {
// do something here
return false;
},
Otherwise,
onWillPop: () async {
// do something here
return true;
},
Returning true will not disable the default behavior of the android back button.

Flutter - set result to return when the user navigates back

When calling Navigator.pop() a result can be passed to the previous screen. Is there a way to set the result so that if the user navigates back on their own the result is still returned to the previous page?
I could pass an object to the second page and then modify it so that the first page can check it when the second page returns, but I'd rather use the result returned from the Navigator as it's more readable.
Overriding the back button's tap detector as I've seen suggested elsewhere is not an acceptable solution because the user may navigate back in some other way such as swiping or pressing the Android back button.
Yes, you can pass data between screens both ways.
While popping back, send the data you wish to send like this from the second screen
RaisedButton(
onPressed: () {
// The Nope button returns "data" as the result.
Navigator.pop(context, 'data');
},
child: Text('Nope!'),
);
And catch the result in the your first screen like this
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
source
For the other concern where the user is able to go back to the previous screen by pressing the back button, you can wrap your second screen with WillPopScope widget and provide the onWillPop callback. This will override the popping of the second screen and execute the callback where you can define the value you wish to return from the second screen.
#override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(), // or your widget
onWillPop: () {
return Future.delayed(Duration(microseconds: 0), () {
return Navigator.pop(context, "return-data");
});
},
);
}
Yes, this is possible, when navigating to Screen B from Screen A, make the onTap() functionasynchronous and await the result from Screen B.
Check the code below: It works perfectly:
On Screen A, put this code:
onTap: () async {
// receive the data you are sending from screen B here
final result = await Navigator.push( context);
},
On Screen B, put this code:
onTap: (){
// pass the data you want to use in screen A as the second paramter
Navigator.pop(context,number);
},
I hope this helps

Hide fingerprint screen when coming back from Home screen

i have app with two screens,one fingerprint screen and another home screen.once
fingerprint is authenticated i will get routed to home screen(using navigator.push()),but if I click back button in phone now it is getting routed to fingerprint screen again.Now how to close the app instead of routing back to finger print screen?
Use Navigator.pushReplacement. It will replace the current topmost widget on the stack with the new one instead of simply pushing it on top.
Example usage -
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
More documentation about this method can be found here.
put your homepage scaffold inside WillPopScope as a child
WillPopScope(
child: Scaffold(...),
onWillPop: () async {
return false;
},
),

Flutter:How to create a full screen mask view

Now I'm using the showModalBottomSheet,
when I click on the semi-transparent bottomsheet, it pops this view out, but what I want is that it doesn't respond to the user's click and still stays there, how do I do that?
How to create a full screen mask view?
If user click on - semi-transparent area - ModalBottomSheet won't close using - WillPopScope
showModalBottomSheet(
context: context,
builder: (context) {
return WillPopScope( // add this
onWillPop: () async => false,
child: Container()); // your code continues
});