22

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?

2

4 Answers 4

47

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.
 }
Sign up to request clarification or add additional context in comments.

11 Comments

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.
@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?
That is right, I replace it as like you mention above using the leading property. The callback is firing.
@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
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.
|
19

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

1 Comment

Way easier than the accepted answer. Thanks.
5

WillPopScope is deprecated after v3.12.0 onwards use Use PopScope instead

  return new PopScope(
      canPop: false,
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text("Some Page"),
        ),
      ),
    );

Comments

1

WillPopScope has been deprecated since Flutter 3.13. The recommended replacement is PopScope, which offers improved support for both Material and Cupertino apps, and works with back gestures and other navigation methods across platforms.

This example shows how to use PopScope to control back navigation based on the current step in a multi-step flow:

PopScope(
  canPop: viewModel.currentStep == 0, // Only allow pop at first step
  onPopInvokedWithResult: (didPop, result) {
    viewModel.back(); // Custom back logic for internal navigation
  },
  child: CupertinoPageScaffold(
    // Your UI goes here
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.