Removing the default back arrow on certain pages - flutter

From my understanding that a back arrow appears after navigating to any page by default. is there a way not to include the back arrow on a certain page of my choice ?

To hide the back button, set the automaticallyImplyLeading property of the Appbar as false
new Scaffold(
appBar: new AppBar(
automaticallyImplyLeading: false,
)
)

Duplicate of https://stackoverflow.com/a/44978595/3013538
I believe the solutions are the following
You actually either:
Don't want to display that ugly back button ( :] ), and thus go for :
AppBar(...,automaticallyImplyLeading: false,...);
Don't want the user to go back - replacing current view - and thus go for:
Navigator.pushReplacementNamed(## your routename here ##);
Don't want the user to go back - replacing a certain view back in the stack - and thus use:
Navigator.pushNamedAndRemoveUntil(## your routename here ##, f(Route<dynamic>)→bool);
where f is a function returning truewhen meeting the last view you want to keep in the stack (right before the new one);
Don't want the user to go back - EVER - emptying completely the navigator stack with:
Navigator.pushNamedAndRemoveUntil(## your routename here ##, (_) => false);
Cheers

According to AppBar docs
If the leading widget is omitted, ... Otherwise, if the nearest Navigator has any previous routes, a BackButton is inserted instead.
So you can hide it in such way
new Scaffold(
appBar: new AppBar(
title: new Text("Without back button"),
leading: new Container(),
),
);

Set automaticallyImplyLeading to false in AppBar or assign empty container in leading
Using automaticallyImplyLeading
appBar: new AppBar(
title: new Text("Your Text Here"),
automaticallyImplyLeading: false,
),
Using empty Container
appBar: new AppBar(
title: new Text("Your Text Here"),
leading: new Container(),
),

In case you want to remove the back button in a FloatingSearchBar, use
automaticallyImplyBackButton: false

Related

bloc eventController is closed unexpected

i´ve an app that should support responsive design.
In the mobile view I am using the scaffold drawer for the main menu:
return Scaffold(
appBar: AppBar(
title: title,
),
drawer: MainMenuDrawer(),
body: body,
);
In the desktop view the menu should always be visible, so i don´t use the drawer propery. Instead of that i´ve added the same menu as widget into a row:
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true ,
title: title
),
body: Row(
children: [
MainMenuDrawer(),
Expanded(child: body),
],
),
);
The menu has a logout button which sends an event to my authentication bloc and pops all pages from the navigation stack, to get to the first page (login page).
authenticationBloc.add(AuthenticationEvent.loggedOut());
while (Navigator.canPop(context)) {
Navigator.pop(context);
}
The authentication bloc is as singleton:
#singleton
class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState>
As long as i am using the mobile view, I can logout from everywhere and it´s works as expected.
But when I switch to the desktop view the logout button doesn´t work anymore.
In the debug mode, I can see, that the eventController of the authBloc is already closed, so no event is added:
void add(Event event) {
if (_eventController.isClosed) return; <- isClosed is true
Does anyone know why this happened in the desktop view?
i´ve found the problen In the onTap method of my listitem.
To close the drawer i´ve called the Navigator.pop(context).
But this will cause the previous page to be closed when the menu is used as drawer.

Why AppBar's automaticallyImplyLeading has no effect?

AppBar(
automaticallyImplyLeading: false, // Has no impact of true/false
title: Text('AppBar'),
)
Docs mention:
automaticallyImplyLeading: If true and leading is null, automatically try to deduce what the leading widget should be. If false and leading is null, leading space is given to title.
In my code, leading is null but setting automaticallyImplyLeading property has no effect on the title. But docs say something else.
automaticallyImplyLeading: false used for removing the back button, from the second screen(This comes when the user navigates from the first screen to the second screen, which is the flutter default behavior). To show back button/leading widget flutter occupy some space at the left side of AppBar.
If false and leading is null, leading space is given to title.
As per the above statement, if it is false then, the default space of the leading widget/back button given to the title widget of App Bar.
appBar: AppBar(
automaticallyImplyLeading: false, // Remove back button
title: const Text('This is simple Text of App Bar for second screen'),
),
With automaticallyImplyLeading: false,

How to change default back button icon in Flutter?

How I will change the back button icon in flutter using the theme. So it can be reflected throughout the application. I saw multiple options in the theme to change the size and color of the icon. But didn't see the change icon option.
Example:
Scaffold(
appBar(
leading: BackButton(), // icon depends on TargetPlattrom
),
),
Or you can create your own constant widget basis on IconButton with your own icon settings, than use it as value for leading property. In addition, there is an AppBarTheme class, which can be used when you create instance of ThemeData.
https://api.flutter.dev/flutter/material/AppBarTheme-class.html
P.S. If you look at AppBar source code you see that regular widget CloseButton or BackButton are used. So there is no specific style for it. And you have 2 ways:
Create your own global widget and use it in each Scaffold.
Subclass Scaffold with your own leading property and use it.
appBar: AppBar(
automaticallyImplyLeading: false,
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(Icons.alarm_on), // Put icon of your preference.
onPressed: () {
// your method (function),
},
);
},
),
title: Text(widget.title),
centerTitle: true,
The above code will change the default back icon to alarm on icon. You can change it to any thing.
I'm not sure if this is the right way to do this. I'm still learning and I don't know if this will work in all cases. I've done this using VS Code while my app is in development.
I looked up the backbutton references(Shift + Alt + F12) and found the actual button in flutter sdk. I changed it there in the back_button.dart file which can be found in your flutter installation folder : c:\flutter\packages\flutter\lib\src\material\back_button.dart
There, edit the static IconData method of the button and choose any Icon you wanna use for any platform.
static IconData _getIconData(TargetPlatform platform) {
switch (platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return Icons.arrow_back_ios;
}
}
In the above case Icons.arrow_back_ios will be returned in all platforms. You can specify the return Icon for each platform.
However, I haven't tested this in like a real world "production use" situation so I hope someone else here can clarify.
This will do the job for you:
IconButton(
onPressed: (){
Navigator.pop(context);
},
icon:Icon(Icons.arrow_back_ios),
//replace with our own icon data.
)
For the future seekers like me.

How to set default text style for appbar title in Flutter?

I have a lot of screens that have an app bar, I always have to set the style for the app bar each time I create a new screen, it so boring. So, anyone knows how to set a custom app bar text style or if there is a solution to this problem?
So you can do is create a method Method as mentioned below and then pass the value to it and later you just call these method where you want just check the code below:
you make another helper_widgets.dart and add the below method so that you can access it everywhere.
Widget appBar(String title,/* you can pass various paramenters to appbar text customization such as fontsize and many more*/ ) {
return AppBar(
title: Text(
title,
style: TextStyle(color: Colors.red, fontSize: 14),
),
);
}
and later just call it in the AppBar Widget
Scaffold(
appBar: appBar('Sample Application'),
);
Let me know if it works.

How to remove the hamburger button from a flutter drawer?

I've seem examples on how to set up a drawer in Flutter (return new Scaffold(drawer: new Drawer( ... ) or return new Scaffold(endDrawer: new Drawer( ... )).
How can I remove the hamburger button at the top (so that you can only get the drawer through sliding from the side (or through a custom button in the app - that I know how to do))?
In AppBar you need to do the following to hide default rendering hamburger icon
AppBar(
automaticallyImplyLeading: false, // this will hide Drawer hamburger icon
actions: <Widget>[Container()], // this will hide endDrawer hamburger icon
... // other props
),
and In SilverAppBar do the following to hide default rendering hamburger icon
SilverAppBar(
automaticallyImplyLeading: false, // this will hide Drawer hamburger icon
actions: <Widget>[Container()], // this will hide endDrawer hamburger icon
... // other props
}
I hope this will help...
Just set the leading property in your AppBar to an empty Container
appBar: new AppBar(
leading: new Container(),
....
And in order to remove endDrawer (for RtL). It is placed where the action property is, so also just add an empty Container as a single child of the action property
appBar: new AppBar(
actions: <Widget>[
new Container(),
],
.....
Use the https://docs.flutter.io/flutter/material/AppBar/automaticallyImplyLeading.html property on the AppBar
For the normal drawer you should set https://docs.flutter.io/flutter/material/AppBar/automaticallyImplyLeading.html to false.
For the end drawer you should do the following:
actions: [Container()]
Simply add this to your Appbar automaticallyImplyLeading: false,
If you have stacked the AppBar() widget over the top of the SliverAppBar() you need to do automaticallyImplyLeading: false in SliverAppBar() widget only.
This will remove the Hamburger icon from the AppBar.
By the way, why it is happening as such? Could someone tell me?
Am not getting it....