How to convert a "FlatButton" to "TextButton" in Flutter? - flutter

I got following code:
FlatButton.icon(
minWidth: double.infinity,
padding: EdgeInsets.symmetric(
vertical: kDefaultPadding,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: kPrimaryColor,
onPressed: () {},
icon: WebsafeSvg.asset("assets/Icons/Edit.svg", width: 16),
label: Text(
"New message",
style: TextStyle(color: Colors.white),
),
).addNeumorphism(
topShadowColor: Colors.white,
bottomShadowColor: Color(0xFF234395).withOpacity(0.2),
),
It seems the FlatButtun is now deprecated and I must convert it to TextButton but when I try that, it gives me errors for minWith parameter. When I try to wrap it with ConstrainedBox I get other errors for padding shape, etc.
I don't know how make this old code work as expected before?

As I've commented, you can't just "convert" a FlatButton.icon to a TextButton.icon. The changes in the buttons are breaking changes made to Flutter:
A new set of basic material button widgets and themes have been added
to Flutter. The original classes have been deprecated and will
eventually be removed. The overall goal is to make buttons more
flexible, and easier to configure via constructor parameters or
themes.
So, to solve your problem, you'll have to compose your own widgets to get close to FlatButton.icon.
For your example, you can use the Padding widget for padding, and SizedBox for the width. for rounded corners, you can use the style property.
Your button code can look something like this using TextButton.icon:
SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 10,
),
child: TextButton.icon(
//rounded corners
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),
onPressed: () {},
icon: WebsafeSvg.asset("assets/Icons/Edit.svg", width: 16),
label: Text(
"New message",
style: TextStyle(color: Colors.white),
),
),
),
)

Related

Flutter borders

Hello I very new to Flutter and I am trying to create a button, but I have a problem with this.
This is the code:
Align(
alignment: AlignmentDirectional(-0.03, 0.13),
child: FFButtonWidget(
onPressed: () {
print('Login pressed ...');
},
text: 'Login',
options: FFButtonOptions(
width: 130,
height: 40,
color: FlutterFlowTheme.of(context).primaryColor,
textStyle: FlutterFlowTheme.of(context).subtitle2.override(
fontFamily: 'Poppins',
color: Colors.white,
),
borderSide: BorderSide(
color: Colors.transparent,
width: 1,
),
borderRadius: BorderRadius.circular(8),
),
),
),
This is the error:
The argument type 'BorderRadius' can't be assigned to the parameter type 'double?'.
Do you have any suggestion?
I think you are using FlutterFlow.
Here you don't need to give the border radius like this
borderRadius: BorderRadius.circular(8)
Do it like this
borderRadius: 8
This is because FlutterFlow button widget is not like the usual Flutter button widget. They are modified versions of actual Flutter widgets.
In your case, properties of FFButtonWidget is set using this class
FFButtonOptions()
This class probably takes borderRadius as a double value and internally sets it like this
borderRadius: BorderRadius.circular(8)
So you have to give the required borderRadius as a double value.
The borderRadius field in the FFButtonOptions require a double parameter. Try
borderRadius: 8.0
It is hard to say, because I do not know how the FFButtonWidget looks like. Normally, your way of setting the border radius would be good for material widgets, for instance:
Align(
alignment: AlignmentDirectional(-0.03, 0.13),
child: Container(
width: 130,
height: 40,
decoration: BoxDecoration(
color: FlutterFlowTheme.of(context).primaryColor,
borderRadius: BorderRadius.circular(8),
),
child: TextButton(
onPressed: () {
print('Login pressed ...');
},
child: Text('Login'),
style: ButtonStyle(
textStyle: FlutterFlowTheme.of(context).subtitle2.override(
fontFamily: 'Poppins',
color: Colors.white,
),
),
),
),
),
Probably the FFButtonWidget takes a double parameter and finally transforms it into
borderRadius: BorderRadius.circular(8.0)
But if you want to use material widgets, you can check how the buttons work in Flutter here.

Parameter named isn't defined

How to fix this parameter name error. It error after I change OutlinedButton. Someone can help me.
Padding(
padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
// ignore: deprecated_member_use
child: OutlinedButton(
padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
textColor: secondaryColor,
borderSide: BorderSide(color: secondaryColor),
child: Text('Send Notification'),
onPressed: () => _sendNotificationDialog(context),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
),
),
FlatButton, Raised button and some widgets are now deprecated from the flutter framework. They wanted to distinguish the styling of the button from the rest of the widget. As you can see hear: New buttons and button themes
Outlined button is not deprecated. To give style to it, you need to override the parameter 'style', like the code below:
OutlinedButton(
style: OutlinedButton.styleFrom(
padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
side: BorderSide(
color: secondaryColor,
),
),
),
child: Text('Send Notification'),
onPressed: () => _sendNotificationDialog(context),
),
What did you had before OutlinedButton ?
The code error means that the parameters you try to assign, doesn't exist in the OutlineButton Widget
Edit :
What I've found is that the buttons were migrated. it could be useful for other of your buttons :
See the migration guide in flutter.dev/go/material-button-migration-guide). ' 'This feature was deprecated after v1.26.0-18.0.pre.'
You can try to use style argument instead. You'll find plenty of parameters to edit your button
OutlinedButton(
style: ButtonStyle(
shape: ,
textStyle: ,
), onPressed: () { },
child: const Text('Hello'),
)

Can't set primary colour of ElevatedButton in Flutter

I have an elevated button in Flutter, generally the button works fine.
However, the setting of the primary: Colors.red in this example does not work, the button is transparent for some reason... All the other params of the ElevatedButton.styleFrom() are working as expected.
Any assistance would be appreciated!
Widget _btnGood() {
return SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(
onPressed: null,
style: ElevatedButton.styleFrom(
primary: Colors.purple,
side: const BorderSide(width: 8.0, color: Colors.white),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(32)),
padding: const EdgeInsets.all(25),
),
child: const Text(
"Im Feeling Good",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
);
}
Example of Button
Simply change on pressed to-
on_pressed: (){} instead of null.

Flutter TextButton Remove Padding and Inner Padding [duplicate]

This question already has answers here:
Flutter: Remove padding in buttons - FlatButton, ElevatedButton, OutlinedButton
(11 answers)
Closed last year.
I just updated my code in Flutter to use TextButton instead of old FlatButton. I can't figure out how to set width and height of a button though.
I got two problems. First one is that I have this icon Button now:
TextButton.icon(
label: Container(),
style: TextButton.styleFrom(padding: EdgeInsets.all(0),
backgroundColor: Colors.black26),
icon: Icon(Icons.share, color: Theme.of(context).primaryColor),
onPressed: () {}),
which loos like this:
I can't figure out how to get rid of the padding on the left and the right. Although I did set the padding inside the style to zero.
My Second Problem is a button that I had like this:
ButtonTheme(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
height: 10,
minWidth: 15,
padding: EdgeInsets.only(top: 5, bottom: 5, right: 5, left: 5),
child: FlatButton(
color: Colors.white.withOpacity(0.9),
child: <MyChild>,
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
side: BorderSide(
color: condition
? Theme.of(context).primaryColor
: widget.color != null
? widget.color
: Colors.black54,
width: 0.5)),
));
}
and it looked like this:
Now I updated my code to this:
OutlinedButton(
style: OutlinedButton.styleFrom(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
padding: EdgeInsets.only(top: 0, bottom: 0, right: 5, left: 5),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
side: BorderSide(
width: 0.5,
color: condition
? Theme.of(context).primaryColor
: widget.color != null
? widget.color
: Colors.black54),
primary: Colors.white.withOpacity(0.9),
),
child: <MyChild>,
onPressed: () {})
But it looks like this now:
The padding on top/bottom is too much but I can't figure out how to minimize it.
Any advice? Thank you!
Edit: I tried to use OutlinedButtonTheme but this did not allow me to set a height etc.
Flutter TextButton is the new button. Since Flutter 2.0 FlatButton is deprecated.
Example of how to use this button with custom styles.
This is a back button with an icon.
It has a wide pressable area and alignment to left according to design.
For inner padding just use Padding widget in the child property - it gives you consistent style for any String length.
TextButton(
onPressed: () => Navigator.pop(context),
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(50, 30),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
alignment: Alignment.centerLeft),
child: Icon(
CupertinoIcons.back,
color: Colors.black,
size: 18,
),
),
For those who are curious about tapTargetSize: MaterialTapTargetSize.shrinkWrap, property - more info is here https://stackoverflow.com/a/71841707/7198006
You need to set three attributes: minimumSize, padding and tapTargetSize
TextButton(
onPressed: (){},
child: Icon(Icons.delete, color: Colors.black54),
style: TextButton.styleFrom(
minimumSize: Size.zero,
padding: EdgeInsets.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
)
Edit:
I have added some padding in my code, and it looks good.
padding: EdgeInsets.fromLTRB(10.0, 3.0, 10.0, 3.0),
Okay, I just figured it out. One need to set the minimumSize attribute.
OutlinedButton.styleFrom(
minimumSize: Size(widthValue, heightValue),
)
This was what made my Buttons bigger than I wanted.

How to set background color for an icon button?

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,
),
...
);