Robert
Robert

Reputation: 5855

Flutter how to execute when clicking back button

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.

Upvotes: 31

Views: 65839

Answers (5)

Florian K
Florian K

Reputation: 2148

WillPopScope is depcreated since v3.12.0-1.0.pre

Use instead PopScope to execute code before pop :

PopScope(
  canPop: true,
  onPopInvoked : (didPop){
   // logic
  },
)

Upvotes: 0

Johan
Johan

Reputation: 425

In my case, just invoking whenComplete() is enough. Take a look of the example :

Navigator.push(context,
        MaterialPageRoute(builder: (context) => const YourRoute()))
    .whenComplete(() {
  // Do something when back to previous state
});

Upvotes: 0

adarsh
adarsh

Reputation: 531

//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);
 }

Upvotes: 2

novas1r1
novas1r1

Reputation: 1934

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 :)!

Upvotes: 44

Shady Aziza
Shady Aziza

Reputation: 53307

You can override the default back arrow on the AppBarand 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.

Upvotes: 31

Related Questions