Custom Back button flutter - flutter

Hi guys i want to chage the beheviour of exiting back button funcionality of Android's defaul bottom bar. In my app if i press the "arrow" it close the application but i want to it come back to previous screen. I don't know how to do it i tried with appBar but using onPressed () appears a black screen. Altrought my mind the best and elegant solution is to change the "arrow" beheviour and it also has to work for IOS. How can i do it?
This is the appBar code
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
color: red,
),
),

if you want to change the behaviour of the bottom back button of app you can use onWillPop
WillPopScope(
onWillPop: () => backPress(),
child: Scaffold(
......
and backpress function decides what to do instead
Future<bool> backPress() async {
print("hellp");
return false;
}

Related

How to add back icon image in app bar in flutter

In Flutter i need to add back icon image in app bar in flutter. I m new to flutter learning & developing any help would be appreciated.
I need to customise the back button image in app bar
Please let me know How to add back icon image in app bar in flutter?
Back IconButton is automatically enabled when you Navigate from another screen.
By using this code
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
If you already Navigated from another screen and are still not showing then slightly change the AppBar field
AppBar(
automaticallyImplyLeading:true,
)
There is a widget call : BackButtonIcon()
AppBar(
leading: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: const BackButtonIcon(),
),)
if you use an AppBar widget in your Scaffold, the back arrow icon will be displayed automatically when you push a new page with the Navigator.
If you want to add manually the icon, you can add it in the leading property inside the AppBar:
AppBar(
leading: IconButton(
icon: const BackButtonIcon(),
onPressed: () { },
)
);

Floating action button to be ontop of drawer after opening

I have a floating action button which opens the drawer. I want it to stay in its place when it opens the drawer and just change the icon to close icon with animation. I couldn't find a good solution to do this.
I tried wrapping inside my drawer with scaffold and adding a floating action button there and using hero widgets so make it look like it stays in its place but didn't help at all.
Is there anyway to achieve this?
U can use foldable Sidebar package to achieve this functionalities that's package is available on :
https://pub.dev/packages/foldable_sidebar
And check this piece of code how to use this :
child: Scaffold(
body: FoldableSidebarBuilder(
drawerBackgroundColor: Colors.deepOrange,
drawer: CustomDrawer(closeDrawer: (){
setState(() {
drawerStatus = FDBStatus.FDB_CLOSE; // For Closing the Sidebar
});
},),
screenContents: FirstScreen(), // Your Screen Widget
status: drawerStatus,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.deepOrange,
child: Icon(Icons.menu,color: Colors.white,),
onPressed: () {
// To Open/Close Sidebar
setState(() {
drawerStatus = drawerStatus == FDBStatus.FDB_OPEN ? FDBStatus.FDB_CLOSE : FDBStatus.FDB_OPEN;
});
}),
),
),

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: How can I catch a back button press to exit my app

I am developing an app that mainly just displays information from a web service to the user (info created by their admin). On the Drawer Menu there are 5 screens to display different information.
After the user has logged in, I use Navigator to move between screens by using pushReplacement. Meaning the current screen is always at the bottom of the screen stack. If the user presses the back button on their device, the app exists immediately.
How can I catch this, so I can ask the user if they want to log out, and then display the login screen?
Thanks for any suggestions.
Cheers,
Paul
You can do this with the WillPopScope widget and changing the onWillPop parameter. The example I posted does something similar to what you want as it shows an alert dialog that can determine what will happen next. The FlatButton onPressed code probably isn't exactly what you're looking for and you'll have to handle that according to your implementation This is the example code is from #AironTark answer to this related question.
#override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(...),
onWillPop: () => showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
title: Text('Warning'),
content: Text('Do you really want to exit'),
actions: [
FlatButton(
child: Text('Yes'),
onPressed: () => Navigator.pop(c, true),
),
FlatButton(
child: Text('No'),
onPressed: () => Navigator.pop(c, false),
),
],
),
),
);
}

How to intercept back button in AppBar in 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).