I have a page that is called from bottom tab nav which executes a initState function, I then navigate to a page via button click that has details and actions to take, however when I click the back button to goto originating page, the initState does not run again, I have found out that because Flutter does not destroy the page when you put one on top, since the NAV is not on new page as its not in the menu, how do I make initState run again on clicking back button or is there another way to listen for that navigate from back button and run the code that needs to update data?
Any help would be great.
By tapping the Android back-button (or the "pop" button) each square turns blue, one by one. Only when all squares are blue, tapping the back-button once more will return to the previous screen.
To add custom back button in Flutter AppBar You Just need to Use Leading in AppBar and Use IconButton for leading Just like this. To add custom back button in Flutter AppBar You Just need to Use Leading in AppBar and Use IconButton for leading Just like this.
onWillPop: onWillPop is a callback method that returns a Future value; if true, the screen can be popped; if false, the screen will not be popped out. However, the screen can still be popped by calling the Navigator. pop(context).
You can listen to the pop with WillPopScope (Creates a widget that registers a callback to veto attempts by the user to dismiss the enclosing [ModalRoute]. -> from documentation):
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
print('Backbutton pressed (device or appbar button), do whatever you want.');
//trigger leaving and use own data
Navigator.pop(context, false);
//we need to return a future
return Future.value(false);
},
child: Scaffold(
...
),
);
}
Hopefully I got your question right and that helps :)!
You can override the default back arrow on the AppBar
and then specify the value you would like to return to trigger the change of the state when Navigator.pop
is called:
Pseudo-Code
so you need to have something like this in your onPressed
callback of your navigation button
onPressed: ()async{
var nav = await Navigator.of(context).push(newRoute);
if(nav==true||nav==null){
//change the state
}
},
and in your newRoute you should have something like this
new AppBar(
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: (){Navigator.pop(context,true)}
),
I am checking for both values null
or true
because the null value is returned when the user hits the BackButton on android screen (the one in the bottom of the screen). I also believe the null will also be returned with the default BackButton in Flutter so you do not actually need to override the leading
property, but I have not checked that myself, so it may be worth checking.
//if you want to perform any operation on back button so you can use WillPopScope Widget and managed your code inside onWillpop method
@override
Widget build(BuildContext context) {
return WillPopScope(
child:Column()),
onWillPop: onBackPress);
}
// this is onBackPress method
Future<bool> onBackPress() {
// your code
return Future.value(false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With