I'm trying to change the border of my OutlinedButton in my main.dart but it doesn't seem to be working. I've been looking around and it seems like I need to add BorderSide. This is what my outlinedButtonTheme looks like:
outlinedButtonTheme: OutlinedButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) {
return AppColors.SecondaryButtonPressed;
}
return AppColors.SecondaryButton;
},
),
minimumSize: MaterialStateProperty.all<Size>(Size(335, 60)),
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
side: BorderSide(
style: BorderStyle.solid,
color: AppColors.Primary,
width: 1), // <-- this doesn't work?
borderRadius: BorderRadius.all(Radius.circular(12)),
)),
foregroundColor: MaterialStateProperty.all<Color>(
AppColors.SecondaryButtonText),
textStyle: MaterialStateProperty.all<TextStyle>(TextStyle(
color: AppColors.SecondaryButtonText,
fontSize: 14 * FONT_SCALE_FACTOR,
fontWeight: FontWeight.w600,
)),
),
),
Shown above where BorderSide is. It doesn't seem like this is working at all.
This works for me:
OutlinedButton(onPressed: () {},
style: ButtonStyle(shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0))),
side: MaterialStateProperty.all(BorderSide(
color: AppColors.blue,
)),
),)
I followed the guide at new material buttons and solved it like this:
OutlinedButton.icon(
style: OutlinedButton.styleFrom(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(5)),
),
).copyWith(
side: MaterialStateProperty.resolveWith<BorderSide>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) return null;
return BorderSide(
color: Theme.of(context).primaryColor,
width: 3,
);
// Defer to the widget's default.
},
),
),
...
I needed to specify a different color for a disabled vs active state because I wanted the border to have a color when its active and no color when its disabled (returning null because by default border has no color unlike the old button)
There were a few things I missed from looking at the docs within ButtonStyle.
I added
side: MaterialStateProperty.all<BorderSide>(
BorderSide(color: AppColors.Primary)),
in ButtonStyle and it worked rather than adding it inside
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
side: BorderSide(
style: BorderStyle.solid,
color: AppColors.Primary,
width: 1), // <-- this doesn't work at all in shape.
borderRadius: BorderRadius.all(Radius.circular(12)),
)),
Use style property:
OutlinedButton(
onPressed: () {},
child: Text('Button'),
style: OutlinedButton.styleFrom(
side: BorderSide(width: 1.0, color: Colors.red),
),
)
Related
I have the problem that the Raised Button is no longer a usable Button in Flutter so I have to replace it with a elevated Button for example.
But when I try to convert it to an elevated Button it gives me an error with the color and shape Property. They seem to belong to the: style Button Style() but I can't convert them. Can someone help me with that.
The code for the Raised Button was:
Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: RaisedButton(
child: Text(
"Gruppen Id kopieren",
style: TextStyle(
color: Colors.white,
),
),
onPressed: () => _copyGroupId(context),
color: Color.fromRGBO(245, 168, 0, 1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
side: BorderSide(
color: Theme.of(context).secondaryHeaderColor,
width: 2,
),
),
),
),
You can set ButtonStyle for ElevatedButton which uses MaterialStateProperty. Now you can set color (or other properties) by checking the state, for example if button is disabled, use
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey.shade600;
}
return Color.fromRGBO(245, 168, 0, 1);
},
),
or you can set a color for all the states like this:
ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.resolveWith(
(states) => RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
side: BorderSide(
color: Theme.of(context)
.secondaryHeaderColor,
width: 2,
),
),
),
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(245, 168, 0, 1),
),
),
onPressed: () => _copyGroupId(context),
child: const Text(
"Gruppen Id kopieren",
style: TextStyle(
color: Colors.white,
),
),
),
You can check the buttons#context
So now instead of RaisedButton we need to useElevatedButton
Similarly, to make an ElevatedButton look like a default RaisedButton:
final ButtonStyle raisedButtonStyle = ElevatedButton.styleFrom(
onPrimary: Colors.black87,
primary: Colors.grey[300],
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2)),
),
);
ElevatedButton(
style: raisedButtonStyle,
onPressed: () { },
child: Text('Looks like a RaisedButton'),
)
Ref and from restoring-the-original-button-visuals
Latest outlined button doesn’t have any splash color property. How to change the splash color? Anyone have any idea
OutlinedButton(
style: OutlinedButton.styleFrom(
onSurface: Colors.green,
side: BorderSide(color: alertColor, width: 1),
),
onPressed: () {},
child: Row(
children: [
FaIcon(
FontAwesomeIcons.trash,
size: 14,
color: alertColor,
),
SizedBox(
width: 10,
),
Text(
'Delete',
style: getFont(14, FontWeight.w500, alertColor),
)
],
),
)
You can change the splash color of the OutlinedButton by changing the style
OutlinedButton.icon(
onPressed: () {},
icon: Icon(Icons.add),
label: Label(
text: 'Add',
),
style: OutlinedButton.styleFrom(
primary: Colors.pink, // <- this changes the splash color
side: BorderSide(width: 1, color: Colors.pink),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
Okay my mistakes. The primary properties in OutlinedButton.styleFrom() can change the splash color.
Use ButtonStyle instead of OutlinedButton.styleFrom
style: ButtonStyle(
overlayColor:
MaterialStateProperty.resolveWith<Color>((states) {
if (states.contains(MaterialState.pressed)) {
return Colors.orange.withOpacity(.1);
}
return Colors.transparent;
}),
Since RaisedButton has been changed for ElevatedButton in flutter 2.0 I try to create a default theme for the button but I can't figure how I can do it.
Here is what I put in my main()
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20))),
),
and I get the following error
The argument type 'RoundedRectangleBorder' can't be assigned to the parameter type 'MaterialStateProperty<OutlinedBorder>'.
I cannot figure how to use the MaterialStateProperty parameter since it doesn't give me any clue on how to use it. I read the documentation but there is no example on this.
Anyone know?
You can refer to this documentation on ButtonStyle to know which MaterialStateProperty fields there are to use.
For example, to define the shape, you can use the following code:
shape: MaterialStateProperty.resolveWith<OutlinedBorder>((_) {
return RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)))
);
}),
Full example:
ElevatedButtonTheme(
data: ElevatedButtonThemeData(
style: ButtonStyle(
side: MaterialStateProperty.resolveWith<BorderSide>(
(states) => BorderSide(color: borderColor ?? Colors.black)),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(states) => Colors.white),
shape: MaterialStateProperty.resolveWith<OutlinedBorder>((_) {
return RoundedRectangleBorder(borderRadius: BorderRadius.circular(20));
}),
textStyle: MaterialStateProperty.resolveWith<TextStyle>(
(states) => TextStyle(color: Colors.red)),
),
),
child: ElevatedButton(onPressed: () {}, child: Text('label')),
);
Try this
ElevatedButton(
onPressed: () {},
child: Text("Button"),
style: TextButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20))),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: TextButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20))),
),
try this
ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
side:
BorderSide(width: 1.0, color: Color.black),
borderRadius:
BorderRadius.circular(5.0))),
backgroundColor: MaterialStateProperty.all<Color>(Color.red),
foregroundColor: MaterialStateProperty.all<Color>(Color.green),
elevation:
MaterialStateProperty.all<double>(8.0),
padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
const EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 5.0))),
onPressed: (){},
child: Text('Tap Me'))
You can use this guide from the Flutter team
https://docs.google.com/document/d/1yohSuYrvyya5V1hB6j9pJskavCdVq9sVeTqSoEPsWH0/edit
but this is the correct code to make ElevatedButton looks like RaisedButton
final ButtonStyle raisedButtonStyle = ElevatedButton.styleFrom(
onPrimary: Colors.black87,
primary: Colors.grey[300],
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2)),
),
);
ElevatedButton(
style: raisedButtonStyle,
onPressed: () { },
child: Text('Looks like a RaisedButton'),
)
I am having some issue changing the icon in a Filter chip in Flutter.
I would like to add a "+" when displaying the widget. Then, if the user selects the chip, then change the icon to a checkmark.
As you can see from the first picture, the icon is changed to a "check", but there is a white checkmark underneath.
How can I remove this extra checkmark?
Code Snippet
FilterChip(
avatar: selected.contains(ms.items[i].text)
? Icon(Icons.check, color: Palette.BLUE)
: Icon(Icons.add, color: Palette.GREY),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
side: BorderSide(
color: Palette.BLUE,
),
),
backgroundColor: Palette.WHITE,
padding: const EdgeInsets.all(8.0),
label: Text(
ms.items[i].text,
style: TextStyle(
fontSize: 16.0,
height: 1.4,
fontWeight: FontWeight.normal,
color: Palette.GREY,
),
),
Current Display
Desired Display
Initial display
You need to set showCheckmark to false. Also, change the border and text color according to the selection state.
FilterChip(
showCheckmark: false,
avatar: selected.contains(ms.items[i].text)
? Icon(Icons.check, color: Palette.BLUE)
: Icon(Icons.add, color: Palette.GREY),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
side: BorderSide(
color: selected.contains(ms.items[i].text)
? Palette.BLUE
: Palette.GREY,
),
),
backgroundColor: Palette.WHITE,
padding: const EdgeInsets.all(8.0),
label: Text(
ms.items[i].text,
style: TextStyle(
fontSize: 16.0,
height: 1.4,
fontWeight: FontWeight.normal,
color: selected.contains(ms.items[i].text)
? Palette.BLUE
: Palette.GREY,
),
),
selectedColor: Palette.WHITE,
onSelected: ...
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: () {},
),
),