Create a button with rounded border [duplicate] - flutter

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: () {},
),
),

Related

Custom Textbutton shape

How to achieve Textbutton shape like this image
I've tried this
TextButton(
onPressed: () {},
style: ButtonStyle(
shape: MaterialStateProperty.all<BeveledRectangleBorder>(
BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10),
)),
backgroundColor: MaterialStateProperty.all<Color>(Colors.red),
),
child: Text('')),
Use this package make some changes and wrap with InkWell https://pub.dev/packages/shape_of_view_null_safe

Flutter: How to fix and add a border and color to outlined button?

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

How to change border color and width of OutlineButton globally?

I need to change the border color and width of the OutlineButton, I know one way of doing this by directly mentioning it inline as below:
OutlineButton(
child: Text('SIGN IN'),
padding: EdgeInsets.all(8.0),
onPressed: handleSignIn,
borderSide: BorderSide(
color: Colors.white, //Color of the border
style: BorderStyle.solid, //Style of the border
width: 1, //width of the border
),
)
If I do it as mentioned below, it's effecting FlatButton and RaisedButton and this is NOT effecting OutlineButton.
MaterialApp(
title: 'MyApp',
theme: ThemeData(
buttonTheme: ButtonThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
// this effects to FlatButton and RaisedButton
side: BorderSide(
color: Colors.white, //Color of the border
style: BorderStyle.solid, //Style of the border
width: 1, //width of the border
),
),
),
),
);
So, How to change border color and width ONLY of OutlineButton globally (through out the app)?
I don't think you can do for that in OutlineButton. So, what i can suggest you is to create a global theme data in a global file.
var borderData=BorderSide(
color: Colors.white, //Color of the border
style: BorderStyle.solid, //Style of the border
width: 1, //width of the border
)
Then you can easily use it any class just by importing that global file.
OutlineButton(
child: Text('SIGN IN'),
padding: EdgeInsets.all(8.0),
onPressed: handleSignIn,
borderSide: borderData,
)
This will reduce your boiler plate code in your case.
Unless borderSide is explicitly added to the OutlineButton, it will default to a width of 1.0, a style of solid and this color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.12)
You have the option of changing onSurface of your app theme, but that will affect other widgets as well.
You can do what Jay suggested in the other answer, but my preference would be to create a different widget. This way, if you ever want to change anything else to all of your OutlineButtons in the future, you only need to change your custom widget:
import 'package:flutter/material.dart';
class OutlineButton2 extends OutlineButton {
const OutlineButton2({
Key key,
#required VoidCallback onPressed,
Widget child,
}) : super(
key: key,
onPressed: onPressed,
child: child,
borderSide: const BorderSide(
color: Colors.white,
style: BorderStyle.solid,
width: 1,
),
);
}
This class is deprecated, please use OutlinedButton instead
OutlinedButton(
style: TextButton.styleFrom(
primary: primaryColor,
side: BorderSide(color: primaryColor),
minimumSize: Size(200, 50),
),
child: Text('SIGN IN'),
),
try deleting the OutlineButton
Try the code below:
GestureDetector(
child: Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 2.0,
color: Theme.of(context).primaryColor)),
),
child: Text('SIGN IN'),
),
onTap: onPressed,
),
basically the GestureDetector will take the onPressed logic code and the container is gonna give the decoration as you ask from it with the Text inside

how to add a button with icon in Flutter

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

How to left align the OutlineButton icon in Flutter

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