How to left align the OutlineButton icon in Flutter?
Icon can be added as follows, but both icon and text are centered aligned in the button. Is there a way to align the icon to the left and text to the center?
return new OutlineButton.icon(
onPressed: onPressed,
label: new Text(title),
icon: icon,
highlightedBorderColor: Colors.orange,
color: Colors.green,
borderSide: new BorderSide(color: Colors.green),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)));
There are many ways you can do it, but it's not possible using the factory constructor that you mentioned OutlinedButton.icon, you can go deeper and check the source code to see how it build the widget.
You can create your own Widget to put an icon to the left and the text centered.
Also you can use the OutlinedButton widget and pass a Stack/Row as a child, check this sample
OutlinedButton(
onPressed: () => null,
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Icon(Icons.access_alarm)
),
Align(
alignment: Alignment.center,
child: Text(
"Testing",
textAlign: TextAlign.center,
)
)
],
),
highlightedBorderColor: Colors.orange,
color: Colors.green,
borderSide: new BorderSide(color: Colors.green),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)
)
)
I Wrap Text Widget with Expanded widget.
OutlinedButton.icon(
icon: Icon(
Icons.lock,
color: MyAppTheme.accentColor,
size: 20,
),
label: Expanded(
child: Text(
' Login',
style: TextStyle(
color: MyAppTheme.primaryColor,
fontSize: 16),
),
),
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)),
side: BorderSide(color: MyAppTheme.primaryColor)
),
),
You can use OutlineButton.icon, and simply wrap your Text with in Center like this:
return new OutlineButton.icon(
onPressed: onPressed,
label: Center(child: new Text(title)),
icon: icon,
)
If you want the text left-aligned, this also works:
return new OutlineButton.icon(
onPressed: onPressed,
label: Align(alignment: Alignment.centerLeft, child: new Text(title)),
icon: icon,
)
Related
What I wanted to achieve is that the size of the elevated button should fill the entire space. in my case if you look at the picture attached i want "user experience" and "engineering" fill the empty space in blue and in the same time aligned from left and right
I am trying to make the buttons size dynamic inside the warp widget, her my code below
Wrap(
spacing: 10,
alignment: WrapAlignment.spaceBetween,
children: [
for (InterestsClass item in interests)
InterestsButton(
text: item.interestsName,
)
],
),
InterestsButton is just an elevated button that is reusable, but if I add expanded as show below its Incorrect use of ParentDataWidget, since there is no flex parent
Widget build(BuildContext context) {
return Expanded(
child: ElevatedButton(
onPressed: () {
setState(() {
_flag = !_flag;
});
},
child: Text(
widget.text,
style: TextStyle(color: _flag ? kWhite : k34, fontSize: 13),
),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
shape: StadiumBorder(),
primary: _flag ? kBlue : Color(0xffFAFAFA)),
),
);
}
I think u can use an alternative way Chip/ ChoiceChip. These widget is available in Flutter very useful for creating size dynamic button with change style when tap.
See more here
https://api.flutter.dev/flutter/material/Chip-class.html
https://medium.com/flutterdevs/chip-widgets-in-flutter-7a2d3d34597c
Result One
I have it working now. Here is the code:
FilterChip(
label: Text("text"),
backgroundColor: Colors.transparent,
shape: StadiumBorder(side: BorderSide()),
onSelected: (bool value) {print("selected");},
),
Result Two
You can achieve it by this way
Chip(
shape: RoundedRectangleBorder(
side: BorderSide(color: colorThird, width: 1),
borderRadius: BorderRadius.circular(10),
),
backgroundColor: colorSecondary,
deleteIcon: Icon(
Icons.close,
color: Colors.white,
size: 15,
),
label: Text(searchController.recentSearchesList[index], style:
TextStyle(color: Colors.white),),
deleteButtonTooltipMessage: 'erase',
onDeleted: () {
print(index);
},
);
Can someone tell me why my code is not making the border blue, and let it have width of 3.0?
This is what it looks like (L: my app, R: tutorial app):
The code:
class CreateRoomButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return OutlinedButton(
onPressed: () => print('Create Room'),
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
side: BorderSide(
color: Colors.blueAccent[100],
width: 3.0,
),
borderRadius: BorderRadius.circular(30.0),
),
),
),
child: Row(
children: [
ShaderMask(
shaderCallback: (rect) =>
Palette.createRoomGradient.createShader(rect),
child: Icon(
Icons.video_call,
color: Colors.white,
size: 35.0,
),
),
const SizedBox(width: 4.0),
Text(
'Create\nRoom',
style: TextStyle(color: Colors.blueAccent[100]),
),
],
),
);
}
}
Also I have to add this somewhere (but since textColor is depreciated in flutter 2.0, idk what to do with it...):
textColor: Palette.facebookblue,
thx!
just change your OutlinedButton to this:
OutlinedButton(
onPressed: () => print('Create Room'),
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
side: BorderSide(width: 3.0, color: Colors.blueAccent[100]),
)
child: yourChildWidget,
)
Like this and in child you can write your widget
OutlinedButton(
style: OutlinedButton.styleFrom(
side: BorderSide(color: Colors.teal),
),
onPressed: () {},
child: null,
),
I have a button in my app like
And below is the code for the same
Widget loginButtonChild = const Text(
"Log in",
style: TextStyle(
color: Colors.white,
fontFamily: "OpenSans-Regular",
),
);
FlatButton.icon(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.red,
label: loginButtonChild,
icon: Icon(Icons.arrow_forward ),
onPressed: () {
//some function
},
)
I am trying to create a button something like
Can anyone help in this or any suggestions?
First off, FlatButton and RaisedButton are deprecated. Use TextButton and ElevatedButton instead.
If you want to add an icon to a text button, use ElevatedButton.icon or TextButton.icon constructor. It will add the icon to the left of the text.
However if you want to add the icon to the right of the text. swap the icon with text and vice versa. This works because both icon and text params are Widget
// icon before text
ElevatedButton.icon(
icon: Icon(Icons.arrow_forward, size: 16),
label: Text('Icon Button'),
onPressed: () => {},
),
// icon after text
ElevatedButton.icon(
icon: Text('Icon Button'),
label: Icon(Icons.arrow_forward, size: 16),
onPressed: () => {},
),
Live Demo
Use that
RaisedButton(
onPressed: () {},
padding: const EdgeInsets.symmetric(horizontal: 24),
color: Colors.redAccent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Row(
children: <Widget>[
Text("Label", style: TextStyle(color: Colors.white)),
SizedBox(width: 6),
Icon(Icons.chevron_right, color: Colors.white),
],
),
),
FlatButton.icon puts the icon on the left, what you can do is use FlatButton and in the child put a Row with the label + icon, like this:
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.red,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
loginButtonChild,
const SizedBox(width: 8.0),
Icon(Icons.arrow_forward),
],
),
onPressed: () {
//some function
},
)
If you want to have the button with elevation, just change FlatButton for RaisedButton
I want to apply background color for icon button but I don't see an explicit backgroundColor property for it. I want to achieve this:
Currently I was able to achieve till here:
Below is the code so far:
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color(0xFF13212C),
appBar: AppBar(
title: Text('Demo'),
),
drawer: appDrawer(),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
children: <Widget>[
// new Flexible(
new TextField(
style: new TextStyle(
color: Colors.white,
fontSize: 16.0),
cursorColor: Colors.green,
decoration: new InputDecoration(
suffixIcon: new IconButton(
icon: new Image.asset('assets/search_icon_ivory.png'),onPressed: null),
fillColor: Colors.black,
contentPadding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
filled: true,
hintText: 'What Do You Need Help With?',
hintStyle: new TextStyle(
color: Colors.white
)
)
)
// )
]
),
You can use a Circular Avatar with the radius = text field's height/2 or whatever height you prefer.
To figure out text field specs you can visit material.io
So the chunk of code is going to be like the following:
CircleAvatar(
radius: 30,
backgroundColor: Color(0xff94d500),
child: IconButton(
icon: Icon(
Icons.search,
color: Colors.black,
),
onPressed: () {
...
},
),
),
This way you get an Icon button with background color.
I hope this can help you guys.
you can wrap your IconButton with Container and use color property to achieve desire output.
May Following Example help You.
suffixIcon: Container(
color: Colors.green,
child: new IconButton(
icon: new Icon(Icons.search,color: Colors.white,),onPressed: null),
),
You can achieve it with TextButton
TextButton(
style: TextButton.styleFrom(
backgroundColor: colorScheme.primary,
shape: CircleBorder(),
),
child: Icon(
MdiIcons.send,
color: colorScheme.onPrimary,
),
onPressed: () {},
),
The output will look like this:
From the official Flutter docs:
Adding a filled background
Icon buttons don't support specifying a
background color or other background decoration because typically the
icon is just displayed on top of the parent widget's background. Icon
buttons that appear in AppBar.actions are an example of this.
It's easy enough to create an icon button with a filled background
using the Ink widget. The Ink widget renders a decoration on the
underlying Material along with the splash and highlight InkResponse
contributed by descendant widgets.
Tl;dr: IconButton doesn't support background color out-of-the-box. Use this workaround to add the background color and the splash effect when clicking the button:
Ink(
decoration: ShapeDecoration(
color: Colors.blue,
shape: CircleBorder(),
),
child: IconButton(
icon: Icon(Icons.add),
color: Colors.white,
onPressed: () {},
),
),
Live Demo
This is now the officially recommended way to set background color on an IconButton, and the flutter docs have been updated to reflect this.
Hope, this will satisfied you
Ink(
decoration: ShapeDecoration(
color: Colors.red,
shape: CircleBorder(),
),
child: IconButton(
icon: Icon(Icons.delivery_dining),
onPressed: () {
print('pressed');
},
),
),
You can not do that with IconButton widget yet. Good news is that you may replace it with FlatButton like that:
suffixIcon: new FlatButton(
color: Colors.green,
disabledColor: Colors.green[100],
child: Icon(Icons.search)),
color will be used in case onPressed handler is defined, otherwise it will be rendered with disabledColor background.
IconButton does not support that, and RaisedButton recently is deprecated in favour of ElevatedButton, which supports changing background colours as well as shapes, but you cannot easily make it a perfect circle.
So to think out of the box, why not use a FloatingActionButton? They are just widgets too, so they can appear anywhere on the screen. For example, I'm using a FAB in a video chat demo app to toggle cameras.
Example code:
FloatingActionButton(
child: Icon(
Icons.flip_camera_ios_outlined,
color: Colors.white,
),
onPressed: () {
// do your thing here
},
)
And if you aren't happy about its default size, you can always wrap it with a SizedBox to change the width however you see fit.
With latest flutter and material-3 design system, this is just a piece a cake.
IconButton(
onPressed: () { ... },
icon: const Icon(Icons.search),
style: IconButton.styleFrom(backgroundColor: Colors.redAccent),
),
Add a Material
Material(
color:Colors.green
child:IconButton(
icon: Icon(Icons.add),
color: Colors.white,
onPressed: () {},
));
Official solution:
Depends on official documentation of flutter about IconButton Class:
Adding a filled background
Icon buttons don't support specifying a background color or other
background decoration because typically the icon is just displayed on
top of the parent widget's background. Icon buttons that appear in
[AppBar.actions] are an example of this.
It's easy enough to create an icon button with a filled background
using the [Ink] widget. The [Ink] widget renders a decoration on the
underlying [Material] along with the splash and highlight
[InkResponse] contributed by descendant widgets.
So the code will be like this:
Material(
color: Colors.transparent,
child: Ink(
decoration: ShapeDecoration(
color: Colors.white,
shape: CircleBorder(),
),
child: IconButton(
tooltip: "Some Text...",
icon: Icon(Icons.flash_off),
color: Colors.black,
onPressed: () {},
),
),
);
See official example code from flutter, click here ...
Note: Reason to wrap it with Material widget because Ink is drawn
on the underlying Material widget, if you remove it decoration will not appear.
I've used multiple colors to demonstrate You can do as you want. And I say that You put your IconButton into Material widget. This will also solve your Splash Color (which is slightly grey color with some transparency).
If you want it raised, you can use RaisedButton like this:
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 50),
child: RaisedButton(
color: kColorLightGrey,
padding: EdgeInsets.symmetric(
vertical: 12,
),
shape: StadiumBorder(),
child: Icon(Icons.refresh),
onPressed: () {
},
),
),
SizedBox(
height: 38,
width: 38,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.grey,
onPrimary: Colors.red,
padding: EdgeInsets.zero,
shape: const CircleBorder(),
),
onPressed: () {},
child: const Icon(Icons.refresh),
);
You can use FloatingActionButton for such case But you cannot use FAB in a widget for multi use, it shows error. In such case use this code. It will work on images also.
Card(
color: Colors.blue,
shape: CircleBorder(),
child: IconButton(
icon: const Icon(
Icons.edit,
size: 20,
color: Colors.white,
),
onPressed: () {
print('tapped');
},
));
You can solve this with a GestureDetector and a container. I like using this solution because of the variety of controls it gives you from the combination.
Additionally, GestureDetector opens up more interaction events if you want to incorporate those across devices.
GestureDetector(
onTap: () {},
child: ClipOval(
child: Container(
//add extra margin to visual center icon if icon isn't balanced
color: backgroundColor,
width: size,
height: size,
child: Icon(
Icons.question_mark_sharp,
size: size * 0.6, //size to your preference
color: iconColor,
),
),
),
);
Alternatively you can do this from another question circle icon button
RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.white,
child: Icon(
Icons.pause,
size: 35.0,
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
)
ElevatedButton with Theme.
ElevatedButton(
style: ButtonThemes.iconButton,
child: Icon(
icon,
color: color,
),
onPressed: () {},
)
static ButtonStyle get iconButton=> ButtonStyle(
backgroundColor: MaterialStateProperty.all(
backgroundColor,
),
...
);
This question already has answers here:
Create a rounded button / button with border-radius in Flutter
(35 answers)
Closed 3 years ago.
How would you make a FlatButton into a button with a rounded border? I have the rounded border shape using RoundedRectangleBorder but somehow need to color the border.
new FlatButton(
child: new Text("Button text),
onPressed: null,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
)
Example of how button with rounded button would look :
Use OutlinedButton instead of FlatButton.
OutlinedButton(
onPressed: null,
style: ButtonStyle(
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0))),
),
child: const Text("Button text"),
);
FlatButton(
onPressed: null,
child: Text('Button', style: TextStyle(
color: Colors.blue
)
),
textColor: MyColor.white,
shape: RoundedRectangleBorder(side: BorderSide(
color: Colors.blue,
width: 1,
style: BorderStyle.solid
), borderRadius: BorderRadius.circular(50)),
)
Use StadiumBorder shape
OutlineButton(
onPressed: () {},
child: Text("Follow"),
borderSide: BorderSide(color: Colors.blue),
shape: StadiumBorder(),
),
new OutlineButton(
child: new Text("blue outline") ,
borderSide: BorderSide(color: Colors.blue),
),
// this property adds outline border color
So I did mine with the full styling and border colors like this:
new OutlineButton(
shape: StadiumBorder(),
textColor: Colors.blue,
child: Text('Button Text'),
borderSide: BorderSide(
color: Colors.blue, style: BorderStyle.solid,
width: 1),
onPressed: () {},
)
For implementing the rounded border button with a border color use this
OutlineButton(
child: new Text("Button Text"),borderSide: BorderSide(color: Colors.blue),
onPressed: null,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(20.0))
),
If you don't want to use OutlineButton and want to stick to normal RaisedButton, you can wrap your button in ClipRRect or ClipOval like:
ClipRRect(
borderRadius: BorderRadius.circular(40),
child: RaisedButton(
child: Text("Button"),
onPressed: () {},
),
),