How to intercept back button in AppBar in flutter - flutter

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?

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

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

Related

Flutter navigator doesn't show back button on App bar

I'm new to flutter and I'm Having a problem with Navigator , I don't know why it doesn't show back button on the next page (i want to show a back button on the next page app bar) when I use Navigator.push even though I have seen a lot of videos that show the opposite, maybe someone will have the answer here
here's the code I'm Using:
In the appbar there is leading property...
In that leading property you can use any Widget. As your requirement you can use icon Widget to navigate
You have to code the back button in "HomePage" class. In Flutter, when you navigate from page A to page B, the page B will be above page B, something like a stack, but page A and B have to be build on their owns.
A solution that might resolve your problem is to use this code in "HomePage"
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Center(
child: ElevatedButton(
child: Text('Back Button'),
onPressed: (){
Navigator.pop();
},
),
),
);
Navigator.push(context, MaterialPageRoute( builder: (context) => CardScreen() ));
Make sure you are using Navigator.push instead of Navigator.pushReplacement

Return data from a screen on Flutter, without using Navigator.pop

I have a DetailPage that must return something to the code that called it. The documentation Return data from a screen explains that you must call Navigator.pop(context, whateverYouMustReturn) on some button.
So far so good, but what happens if the user clicks on the back button on the AppBar instead?? How do I return something then??
PS I'm using Navigator 1.0, btw
Provide your own leading widget and override the callback.
AppBar(
leading: BackButton(
onPressed: () {
Navigator.of(context).pop(myData);
},
),
),
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Container(), //Make leading an empty container to hide the default back button
),
body: WillPopScope(
onWillPop: () async {
Navigator.pop(context); //This method returns future boolean, based on your condition you can either
return true; //allow you to go back or you can show a toast and restrict in this page
},
child: Container(), //Your details page widget instead of container
),
);
}

Flutter: How to change previous navigation route without actually navigating?

My users will navigate to screen 1, then screen 2 then screen 3.
When they click the back button on screen 3, depending upon a variable in that screen, I want the back button to either take them back to screen 2 or screen 1.
Note: the back button must be trigger by the user, I don't to pragmatically navigate back.
I thought I could use WillPopScope but that can only return a bool.
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
return true
},
child: Scaffold(
...
),
);
}
,
Here's an example:
return WillPopScope(
onWillPop: () async => true,
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
_user.isRegistered
? Navigator.popUntil(context, (route) => route.isFirst) // Takes the user to the first page
: Navigator.push(context, MaterialPageRoute(builder: (context) => SecondRoute()));
},
),

back button inside AppBar showing black screen in flutter

I have bottom tab navigator that contains of A and B screens, inside B screen I have widget that navigate to C page and inside C page I want to add back arrow to go back to previous screen (B screen), so...
I have simple widget that the code looks like this
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop(),
),
title: Text("XXX"),
centerTitle: true,
),
body: detail()));
}
the problem is whenever I click back arrow.. it turns to black screen... I don't get any idea what the problem is... so is there a way to go to my previous screen?
and this is how I ndo navigate from B screen to C page
InkWell(
onTap: () {Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => new NextScreen(check: values)));
},
child: Text(
"Next...",
style: TextStyle(
fontSize: 13.0,
),
),
)
The reason why you see a blank screen is because you navigated using pushReplacement. What pushReplacement does it that it will navigate to the next screen without stacking itself to the route meaning that it will make the app forget that the last screen was your screen B. Try using Navigator.push() instead.
Here is an example:
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ScreenC()),
);
}

Flutter - keeping Drawer after page navigation

I have a Drawer widget on main.dart. The Drawer works great on this page, but when I navigate to a different page, using code like below, I lose the the drawer on the new page. Is there any recommended way to keep the drawer after navigating to a separate page?
Navigator.push(context, new MaterialPageRoute(builder: (context) => new MessageEntryPage()));
I saw similar questions in stackoverflow but couldn't get an answer.
not use Navigator.push
instance of body change
for example
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
drawer: your drawer ,
body: // your Widget
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
When we change the drawer, we use setState to change the widget on the body