When navigating between pages in flutter, if the second page has an AppBar, by default the AppBar comes with an IconButton to go back to the previous page. Is there a way to set a custom icon for that IconButton that is different than the one provided by default by Flutter and will be applied to every other page? Kind of like a custom AppBarTheme?
You can remove the BackButton and handling it by yourself, using:
AppBar(
automaticallyImplyLeading: false,
leading: Navigator.canPop(context)
? IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.black,
size: 47,
),
onPressed: () => Navigator.of(context).pop(),
)
: null,
);
Related
How can I change the function of the back button in dart flutter
I really didn't try anything because tbh, i didn't find what to try
You need simply to override the leading property in the AppBar with a new IconButton, then set your specific method:
AppBar(
leading: IconButton(
onPressed: () {
print("clicked");
},
icon: Icon(Icons.abc),
),
),
flutter, what is the right way to change appbar leading icon at app theme level? so I no need to change on each screen?
I know the following code work for specific screen...
AppBar(
title: Text("Hello Appbar"),
leading: Icon(
Icons.arrow_back,
),
);
but I don't want to do it on each screen, Just checking here if I can set it at app theme level so it is same for all screen...
I referred flutter documentation for appbar, but it was not much helpful.
You can do something like this
import 'package:flutter/material.dart';
AppBar MyAppBar({Widget title, Widget leading}) {
return AppBar(
title: title,
leading: leading,
);
}
Use IconButton to change the leading icon.
AppBar(
title: Text("Hello Appbar"),
leading: IconButton(
onPressed: () {},
icon: Icon(Icons.arrow_back)
),
);
I want to make a custom back arrow with a specific height and width, how can I achieve that in flutter? I tried wrapping the IconButton with SizedBox and Container but none of those work. This is the current back arrow from the Icon(Icons.arrow_back):
But I wanted to make it more like this:
The straight line of the arrow is a little longer and the head of the arrow is a little shorter than the default one. Any suggestions on how to achieve this? Should I use another icon or can I make customize the already given one by default?
The code:
AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
iconTheme: IconThemeData(
color: Colors.black,
),
title: new Text('Back Arrow')
leading: IconButton(
color: Color(0xFF323232),
icon: Icon(
Icons.arrow_back,
),
onPressed: () => Navigator.of(context).pop(),
),
),
Thanks in advance for the help and suggestions!
You cant customise inbuilt icons in flutter, you need to create your own icons. There is size property in Icon widget which grows both width and height but it will not satisfy your need.
You can download icons from below sites:
Flaticon
Icons8
and check this blog to use custom icons in flutter
I am trying to change the size of the icons ("Back" provided by Navigator and "Drawer" provided by Scaffold) in appbar in my ThemeData, modifying iconTheme and primaryIconTheme:
ThemeData(
primarySwatch: Colors.blue,
iconTheme: IconThemeData(color: Colors.grey, size: 32),
primaryIconTheme: IconThemeData(color: Colors.grey, size: 32)
),
This lines only changes the color of the icons, but the size does not change.
I do not need to change the behavior that Flutter handles this button, only the size of them.
The AppBar widget uses a BackButton widget, so that change is not possible unless you modify it from the framework in a different branch, but the best approach is to use the Leading property and the condition ModalRoute.of(context)?.canPop to check whether the current route can be popped. For example:
AppBar(
leading: ModalRoute.of(context)?.canPop == true
? IconButton(
icon: Icon(
Icons.arrow_back,
size: 60,
),
)
: null,
title: Text(
"Example",
),
)
Luis' answer is mostly complete, but note that you will need to define the onPressed callback in the IconButton, shown below:
AppBar(
leading: ModalRoute.of(context)?.canPop == true
? IconButton(
icon: Icon(
Icons.arrow_back,
size: 60,
),
onPressed: () => Navigator.of(context).pop(),
// used onPressed here
)
: null,
title: Text(
"Example",
),
)
How can I create a FloatingActionButton like in the image?
I tried using FloatingActionButton, but it looks like a whole button with a circle.
I need something like an IconButton as shown in the image.
IconButton will actually deliver the result you are aiming for!
A FloatingActionButton, by guidelines, will always deliver the design you find on your "View in Your Room" titled button.
IconButton(
icon: Icon(Icons.back),
onPressed...
)
You need to use IconButton as below
// Icon Button
new IconButton(
icon: new Icon(Icons.favorite),
tooltip: 'Facorite icon',
color: Colors.blue, //set color which you want
onPressed: () {
// Do your work
},
),
For this, you can use actions ** property of appbar to put icons on the right side of title and **leading property to put a leading icon.
appbar: new AppBar(
leading: new Icon(Icons.search),
actions: <Widget>[
new Icon(Icons.search),
new Icon(Icons.search),
],
title: new Text("title"),
)
I suggest using an Icon inside a GestureDetector widget. This will give you the result you want, but also getting rid of the unnecessary padding around an IconButton.
GestureDetector(
onTap: () {},
child: Icon(Icons.arrow_back, color: Colors.black)
)