Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 589
Robert Avatar asked Feb 02 '18 12:02

Robert


People also ask

How do you get the back button event in Flutter?

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.

How do you customize the back button on a Flutter?

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.

How do you use onWillPop in Flutter?

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


3 Answers

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

like image 197
novas1r1 Avatar answered Sep 25 '22 21:09

novas1r1


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.

like image 33
Shady Aziza Avatar answered Sep 24 '22 21:09

Shady Aziza


//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);
 }
like image 23
adarsh Avatar answered Sep 25 '22 21:09

adarsh