How to reduce the size of ElevatedButton without hardcoding the values - flutter

I want to reduce the Width and not hardcode the value by specifying minWidth and minHeight, I want it to look same across all the devices.
ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.arrow_forward_ios_sharp),
label: Text('Plus One'),
)

Use Media Query to use width wisely for your solution which will run the same for small and large screen
Container(
width: MediaQuery.of(context).size.width * 0.5, // Will take 50% of screen space
child: RaisedButton(
child: Text('Go to screen two'),
onPressed: () {},
),
)
You can apply a similar solution to SizeBox also.

Related

Can't center Icon in a TextButton

I'm trying to center the minimized icon in this Icon Button but can't get it to work:
#override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 25,
child: TextButton(
onPressed: appWindow.minimize,
style: const ButtonStyle(
alignment: Alignment.center,
padding: MaterialStatePropertyAll(EdgeInsets.all(0))),
child: const Icon(
Icons.minimize,
color: Colors.white,
),
),
),
TextButton(
onPressed: maximizeOrRestore,
child: Icon(
appWindow.isMaximized ? Icons.fullscreen_exit : Icons.fullscreen,
color: Colors.white,
)),
TextButton(
onPressed: appWindow.close,
child: const Icon(
Icons.close,
color: Colors.white,
),
)
],
);
}
I'm expecting the button to be centered and as you can see i've already tried using alignment and padding
When you say "center the minimized icon", do you mean that this icon should be between the other two icons? In that case, you just need to switch the first two widgets in the Row widget's children.
But I think you want the minimize icon to be higher so that it's something like-> - ◾️ X
If this is what you want then you can't use Icons.minimize. If you check out this icon on this page, you will notice that the minimize icon looks like an underscore. This is by design. I think this looks good, but if you insist on having a minus sign kind of symbol then you can use Icons.remove_rounded.
It's not that the icon is not centered, the Material minimize icon has blank space in the upper size, because it is suppose to be down to understand that is a minimize button just like the maximize button has blank space in the bottom size. What you can try is to use a different icon if you really want it to be centered. Try with Icons.horizontal_rule.
You can use CupertinoIcons.minus like
TextButton(
onPressed: appWindow.minimize,
child: const Icon(
CupertinoIcons.minus,
color: Colors.white,
),
),

How to make IconButton bigger?

I'd like to make IconButton bigger, but this doesn't work.
How to make the size of the button bigger?
SizedBox(
height: 100,
width: 100,
child: IconButton(
tooltip: 'Refresh',
icon: const Icon(Icons.refresh),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Check()),
);
},
),
),
You can set the property iconSize.
Here is the official documentation.
Solution:
IconButton(
iconSize: 100,
tooltip: 'Refresh',
icon: const Icon(Icons.refresh),
onPressed: () {
//Your code goes here....
}
),
You can use the iconSize property...You dont need to use SizedBox when using iconSize
IconButton(
iconSize: 100,
icon: const Icon(Icons.refresh),
onPressed: () {}
),
You can use iconSize property of IconButton() widget, it will help to increase the size. This property increases the size of icon inside it too. So, put the size of Icon() as well so that you can have the desired size of IconButton and Icon both.
IconButton(
iconSize: 100,
tooltip: 'Refresh',
icon: const Icon(Icons.refresh, size:20),
onPressed: () {
},)
IconButton sets height and width internally, with nothing to do with any parent Widget.
IconButton(
iconSize: 100,
internally, IconButton sets the height and width of the icon as 100, and default iconSize is this.iconSize = 24.0,. That's important to make look icons good.

What is the alternative to IntrinsicHeight in flutter?

I have this code in bottomNavigationBar"
bottomNavigationBar: BottomAppBar(
child: IntrinsicHeight(
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () => Navigator.of(context).pop(),
),
Spacer(),
IconButton(
icon: Text(
"QR",
style: Theme.of(context).textTheme.title,
),
onPressed: () => Navigator.of(context).pop(),
),
VerticalDivider(
color: Theme.of(context).textTheme.headline.color,
),
IconButton(
icon: Icon(Icons.share),
onPressed: () => Navigator.of(context).pop(),
),
],
),
),
),
And the code works as expected.
If I remove IntrinsicHeight widget, the divider goes all the way across all screen.
The reason I want an alternative is because in the documentation of IntrinsicHeight it says:
This class is relatively expensive. Avoid using it where possible.
What would be the cheap alternative?
Thank you
If you're looking for "a cheap way to have the row fit the min height of dynamic content", then there are none.
The cheap solution is, to have a fixed height on the Row – typically by wrapping it in SizedBox:
SizedBox(
height: 42,
child: Row(...),
)
This works well if the content has a fixed height. But it won't if the height is dynamic.
In this specific case, you could either use SizedBox with height=48 (this is the default height of the IconButton widget) or avoid using VerticalDivider and draw it by adding a left border to the share icon.
Container(
decoration: BoxDecoration(
border: Border(
left: Divider.createBorderSide(
context,
color: Theme.of(context).textTheme.headline.color,
),
),
),
child: IconButton(
icon: Icon(Icons.share),
onPressed: () => Navigator.of(context).pop(),
),
),
In Flutter it might seem counter-intuitive, but when most widgets are given bounded constraints they try to fill the whole (bounded) space allowed, whereas when given unbounded constraints (set to INFINITY) they only take the required space (their intrinsic size). So to make a widget have its intrinsic size one can try wrapping it with UnconstrainedBox.
But it might be problematic in your case, because you are using a Row, and its height should not be unbounded..
Wrap a widget in a Container with a specific height that's the alternative of Intrinsic Height.

Flutter: Overlap Edge of Widget with second Widget

I want to overlap the bottom edge of a widget with another widget so that it looks like this:
I am using a stack to position the arrow button over the card. At the moment I just set the position with an invisible box above it. The issue is that this method only works for that exact resolution - it should be screen size independent.
The necessary code for the widget:
Stack(
children: <Widget>[
Container( //background
height: 100,
width: 100,
),
FloatingActionButton(
child: Icon(Icons.arrow_forward),
onPressed: () {},
)
],
)
Update - April 2021
You can no longer use the overflow property in a Stack widget as it has been deprecated. Use clipBehaviour instead. Source: Flutter docs
You can use the Stack and Positioned widget to achieve this UI.
Stack(
clipBehavior: Clip.none,
children: <Widget>[
Container(
color: Colors.amber,
height: 150,
width: 150,
),
Positioned(
child: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
print('FAB tapped!');
},
backgroundColor: Colors.blueGrey,
),
right: 0,
left: 0,
bottom: -26,
),
],
),
Output:
Update - March 2022
The Area which is outside of Stack won't be clickable due to the platform limitation, and that is how the Stack widget works.
You can learn more about the same in this issue.
As of November 2019 I'd like to add a second solution:
Using package: https://pub.dev/packages/assorted_layout_widgets
var widget1 = ...;
var widget2 = ...;
ColumnSuper(
children: [widget1, widget2],
innerDistance: -26,
);
The difference from this solution to the one using Stack is that Positioned widgets in a Stack don't occupy vertical space. However, the ColumnSuper will have the size of all of its children widgets. Note I am the author of this package.
You could try my package (signed_spacing_flex). It's exactly the same as a normal Column (or Row and Flex). But it lets you set negative spacing which causes its children to overlap. You can also set which children should be on top when they overlap.
In your case it would be something like:
SignedSpacingColumn(
spacing: -16.0,
stackingOrder: StackingOrder.lastOnTop,
children: [
Container(
height: 100,
width: 100,
color: Colors.red,
),
FloatingActionButton(
child: const Icon(Icons.arrow_forward),
onPressed: () {},
)
],
)
It works with expanded children if you need.

How do I remove Flutter IconButton big padding?

I want to have a row of IconButtons, all next to each other, but there seems to be pretty big padding between the actual icon, and the IconButton limits. I've already set the padding on the button to 0.
This is my component, pretty straightforward:
class ActionButtons extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.lightBlue,
margin: const EdgeInsets.all(0.0),
padding: const EdgeInsets.all(0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
IconButton(
icon: new Icon(ScanrIcons.reg),
alignment: Alignment.center,
padding: new EdgeInsets.all(0.0),
onPressed: () {},
),
IconButton(
icon: new Icon(Icons.volume_up),
alignment: Alignment.center,
padding: new EdgeInsets.all(0.0),
onPressed: () {},
)
],
),
);
}
}
I want to get rid of most of the light blue space, have my icons start earlier on the left, and closer to each other, but I can't find the way to resize the IconButton itself.
I'm almost sure this space is taken by the button itself, 'cause if I change their alignments to centerRight and centerLeft they look like this:
Making the actual icons smaller doesn't help either, the button is still big:
thanks for the help
Simply pass an empty BoxConstrains to the constraints property and a padding of zero.
IconButton(
padding: EdgeInsets.zero,
constraints: BoxConstraints(),
)
You have to pass the empty constrains because, by default, the IconButton widget assumes a minimum size of 48px.
Two ways to workaround this issue.
Still Use IconButton
Wrap the IconButton inside a Container which has a width.
For example:
Container(
padding: const EdgeInsets.all(0.0),
width: 30.0, // you can adjust the width as you need
child: IconButton(
),
),
Use GestureDetector instead of IconButton
You can also use GestureDetector instead of IconButton, recommended by Shyju Madathil.
GestureDetector( onTap: () {}, child: Icon(Icons.volume_up) )
It's not so much that there's a padding there. IconButton is a Material Design widget which follows the spec that tappable objects need to be at least 48px on each side. You can click into the IconButton implementation from any IDEs.
You can also semi-trivially take the icon_button.dart source-code and make your own IconButton that doesn't follow the Material Design specs since the whole file is just composing other widgets and is just 200 lines that are mostly comments.
Wrapping the IconButton in a container simply wont work, instead use ClipRRect and add a material Widget with an Inkwell, just make sure to give the ClipRRect widget enough border Radius πŸ˜‰.
ClipRRect(
borderRadius: BorderRadius.circular(50),
child : Material(
child : InkWell(
child : Padding(
padding : const EdgeInsets.all(5),
child : Icon(
Icons.favorite_border,
),
),
onTap : () {},
),
),
)
Instead of removing a padding around an IconButton you could simply use an Icon and wrap it with a GestureDetector or InkWell as
GestureDetector(
ontap:(){}
child:Icon(...)
);
Incase you want the ripple/Ink splash effect as the IconButton provides on click wrap it with an InkWell
InkWell(
splashColor: Colors.red,
child:Icon(...)
ontap:(){}
)
though the Ink thrown on the Icon in second approach wont be so accurate as for the IconButton, you may need to do some custom implementation for that.
Here's a solution to get rid of any extra padding, using InkWell in place of IconButton:
Widget backButtonContainer = InkWell(
child: Container(
child: const Icon(
Icons.arrow_upward,
color: Colors.white,
size: 35.0,
),
),
onTap: () {
Navigator.of(_context).pop();
});
I was facing a similar issue trying to render an Icon at the location the user touches the screen. Unfortunately, the Icon class wraps your chosen icon in a SizedBox.
Reading a little of the Icon class source it turns out that each Icon can be treated as text:
Widget iconWidget = RichText(
overflow: TextOverflow.visible,
textDirection: textDirection,
text: TextSpan(
text: String.fromCharCode(icon.codePoint),
style: TextStyle(
inherit: false,
color: iconColor,
fontSize: iconSize,
fontFamily: icon.fontFamily,
package: icon.fontPackage,
),
),
);
So, for instance, if I want to render Icons.details to indicate where my user just pointed, without any margin, I can do something like this:
Widget _pointer = Text(
String.fromCharCode(Icons.details.codePoint),
style: TextStyle(
fontFamily: Icons.details.fontFamily,
package: Icons.details.fontPackage,
fontSize: 24.0,
color: Colors.black
),
);
Dart/Flutter source code is remarkably approachable, I highly recommend digging in a little!
A better solution is to use Transform.scale like this:
Transform.scale(
scale: 0.5, // set your value here
child: IconButton(icon: Icon(Icons.smartphone), onPressed: () {}),
)
You can use ListTile it gives you a default space between text and Icons that would fit your needs
ListTile(
leading: Icon(Icons.add), //Here Is The Icon You Want To Use
title: Text('GFG title',textScaleFactor: 1.5,), //Here Is The Text Also
trailing: Icon(Icons.done),
),
I like the following way:
InkWell(
borderRadius: BorderRadius.circular(50),
onTap: () {},
child: Container(
padding: const EdgeInsets.all(8),
child: const Icon(Icons.favorite, color: Colors.red),
),
),
enter image description here
To show splash effect (ripple), use InkResponse:
InkResponse(
Icon(Icons.volume_up),
onTap: ...,
)
If needed, change icons size or add padding:
InkResponse(
child: Padding(
padding: ...,
child: Icon(Icons.volume_up, size: ...),
),
onTap: ...,
)