16

I am using interceptor https://pub.dartlang.org/packages/back_button_interceptor to execute a method when the page 1 is back from page 2.

If I come back from page 2 to page 1 using device back button, the method is executed.

But if I come back from page 2 to page 1 using the arrow button at appBar I am not able to execute the method.

How can the back arrow button functionality default to the device back button?

ningomba 2.0
  • 253
  • 1
  • 2
  • 11
  • 1
    possible duplicate of https://stackoverflow.com/questions/45916658/how-to-deactivate-or-override-the-android-back-button-in-flutter – Mazin Ibrahim Mar 03 '19 at 18:16
  • 2
    Possible duplicate of [How to deactivate or override the Android "BACK" button, in Flutter?](https://stackoverflow.com/questions/45916658/how-to-deactivate-or-override-the-android-back-button-in-flutter) – Marcos Boaventura Mar 03 '19 at 18:45

2 Answers2

42

You can surround your scaffold on Page 2 with WillPopScope, set onWillPop to false to prevent the page from being popped by the system and then add your own back button into the app bar's leading widget and perform your pop in there.

@override
Widget build(BuildContext context) {
  return new WillPopScope(
    onWillPop: () async => false,
    child: new Scaffold(
      appBar: new AppBar(
        title: new Text("data"),
        leading: new IconButton(
          icon: new Icon(Icons.ac_unit),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
    ),
  );
}

code for the answer from this post

Edit: Addition to Page 2 to control navigation

In addition to the above code you'll add the below code to page 2. Change

Navigator.of(context).pop() 

to

Navigator.of(context).pop('upload_files')

Then in your page 1 where you navigate you'll await the navigation and use the result returned from the pop on page 2 and run your logic

var navigationResult = await Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (context) => Page2()));


 if(navigationResult == 'upload_files') {
    uploadFiles(); // Perform your custom functionality here.
 }
Filled Stacks
  • 4,116
  • 1
  • 23
  • 36
  • Thanks for your looking into it, This does not change anything, I want to mention again that I am using interceptor at page 1 to call a method when the page is back from page 2. That method call is working from the device back button. But when I come back from page 2 to page 1 using the back button at AppBar the method is not called. – ningomba 2.0 Mar 04 '19 at 04:03
  • @ningomba2.0 I don't know if you read the answer properly. You replace the back button in the appBar with your own button then you can call what you want in there? Is the onPressed callback firing in your leading widget? – Filled Stacks Mar 04 '19 at 05:19
  • That is right, I replace it as like you mention above using the leading property. The callback is firing. – ningomba 2.0 Mar 04 '19 at 06:07
  • @ningomba2.0 so then the answer is correct? You can just call the same method you want to call from there and handle it as you mention in your first scenario – Filled Stacks Mar 04 '19 at 10:23
  • I will try to upload the files, I think I am not able to explain clearly. I am using interceptor at page 1 and not in page 2. I mentioned that I want to execute a function in page 1 when it is back from page 2. – ningomba 2.0 Mar 04 '19 at 12:08
  • @ningomba2.0 ooooooooooooh. Well that's very different answer. I'll update my answer now for your needs. – Filled Stacks Mar 04 '19 at 15:38
  • @ningomba2.0 I updated the answer for what you want to achieve. – Filled Stacks Mar 04 '19 at 15:44
  • Stacks that is doing the trick, that is exactly I am looking for. Thanks a lot for helping me out. – ningomba 2.0 Mar 04 '19 at 17:26
  • @ningomba2.0 you're very welcome. If you have other flutter questions that's not getting answered contact me in the direct messages and I'll see if I can assist you. – Filled Stacks Mar 04 '19 at 17:38
  • Under AppBar(...) you may need to add automaticallyImplyLeading: false, – Boris Apr 15 '21 at 13:25
  • https://pub.dev/packages/back_button_interceptor – Developer Jan 21 '22 at 12:02
9

The default back button in AppBar is BackButton widget from material.dart. You may create it manually and pass your own onPressed to do what you want:

return Scaffold(
  appBar: AppBar(
    leading: BackButton(onPressed: _onBackPressed),
    title: Text('Title'),
  ),
  body: Container(),
);

If you do not specify leading in AppBar, then it creates a BackButton with the handler that does Navigator.maybePop(context).

Alexey Inkin
  • 1,853
  • 1
  • 12
  • 32