Using MaterialStatePropertyAll in Flutter - flutter

OutlinedButton(
style: ButtonStyle(
padding: MaterialStatePropertyAll(
const EdgeInsets.symmetric(vertical: 10, horizontal: 30)),
shape: MaterialStatePropertyAll(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)))),
onPressed: () {},
child: Text('Text'))
For example, we have a button as in the code below. I need to use MaterialStatePropertyAll to give properties to this button. Do I have to write MaterialStatePropertyAll when I attribute the button each time? Can't we just write this in one go and collect all the OutlinedButton properties under MaterialStatePropertyAll?

You can define this in MaterialApp's theme and all your OutlinedButton get that automatically:
MaterialApp(
theme: ThemeData(
outlinedButtonTheme: OutlinedButtonThemeData(
style: ButtonStyle(
padding: MaterialStateProperty.all(
EdgeInsets.symmetric(vertical: 10, horizontal: 30)),
shape: MaterialStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)))),
),
primarySwatch: Colors.blue,
),
home: ...,
)
then use it in every class that has that material as parent like this:
OutlinedButton(
style: Theme.of(context).outlinedButtonTheme.style,
onPressed: () {},
child: Text('Text'),
)

Related

Raised Button Replacement Flutter Update

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

Flutter transparent button elevation

I created a custom button that I want to give a color and an elevation. Works finde, except when I give the button the color Colors.transparent. Here is an example to showcase my problem:
Widget buildButton(void Function() onPressed, String label, double? elevation) {
return OutlinedButton(
onPressed: onPressed,
child: Text(label),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.white,
side: BorderSide(
width: 2,
color: Colors.blue,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: elevation ?? 0
),
);
}
class ButtonsDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
buildButton(() {}, "Button", null),
buildButton(() {}, "Button", 5),
],
),
),
);
}
}
The buttons look like this:
Here is how they should look like (I set the color of the buttons to the background color of the screen to showcase them, but I can't do that in my code):
I tried this answer but it only got me so far (I don't know if I did it completely wrong, but it looks so):
I also tried this answer and it got me there:
So, as you can see, both of them answers haven't helped me at all. Can anyone show me how I can add elevation to a transparent button?
The best solution to your problem is to use ElevatedButton instead of OutlinedButton and to give it a shadowColor. The code below is for the second button, please give it a try.
ElevatedButton(
onPressed: () {},
child: Text(
'Elevated',
style: TextStyle(color: Colors.black),
),
style: ElevatedButton.styleFrom(
elevation: 5,
primary: Colors.transparent,
shadowColor: Colors.transparent.withOpacity(0.1),
side: BorderSide(
width: 2,
color: Colors.blue,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),

How to make a chip button design in flutter

I am learning flutter. How can I have to make the following design? ignore the container border have to make a chip buttons
This is the button design as you mentioned
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red),
),
),
),
child: Text(
'data',
style: TextStyle(color: Colors.black),
),
);
try to follow this article and you will find your answer
chip-widgets
Widget _buildChip(String label, Color color) {
return Chip(
labelPadding: EdgeInsets.all(2.0),
avatar: CircleAvatar(
backgroundColor: Colors.white70,
child: Text(label[0].toUpperCase()),
),
label: Text(
label,
style: TextStyle(
color: Colors.white,
),
),
backgroundColor: color,
elevation: 6.0,
shadowColor: Colors.grey[60],
padding: EdgeInsets.all(8.0),
);
}
}
chipList() {
return Wrap(
spacing: 6.0,
runSpacing: 6.0,
children: <Widget>[
_buildChip('Gamer', Color(0xFFff6666)),
_buildChip('Hacker', Color(0xFF007f5c)),
_buildChip('Developer', Color(0xFF5f65d3)),
_buildChip('Racer', Color(0xFF19ca21)),
_buildChip('Traveller', Color(0xFF60230b)),
],
);
}

Is ElevatedButton working correctly for the web?

I'm working on my first Flutter project and my goal is to make my app work on all platforms (Android, iOS, Web).
My forehead is slowly becoming more red as I'm working with the ElevatedButton. I can't make it work on the Web.
This is how it looks in Android: (correct)
This is how it looks on the Web: (incorrect)
This is my theme:
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
primary: Colors.black,
onPrimary: Colors.yellowAccent,
side: BorderSide(width: 2.5, color: Colors.yellowAccent),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
textStyle: TextStyle(
fontSize: 15, fontWeight: FontWeight.w600
),
elevation: 6,
shadowColor: Colors.yellowAccent,
padding: EdgeInsets.all(8.0),
),
),
),
This is my button:
Container(
padding: EdgeInsets.all(8.0),
child: ElevatedButton(
child: Text("MY BUTTON"),
onPressed: () => setState(() {
_launched = _launchInBrowser('https://www.mywonderfulsite.com');
}),
)),
Is this a "feature" of ElevatedButton or can it be solved using it?
Appreciate suggestions.
The only solution I've found is to put a fixed width and height of the Container. Something like this:
Container(
width: 124.0, // <-- Added this...
height: 48.0, // <--- ...and this.
padding: EdgeInsets.all(8.0),
child: ElevatedButton(
child: Text("MY BUTTON"),
onPressed: () => setState(() {
_launched = _launchInBrowser('https://www.mywonderfulsite.com');
}),
)),
Hopefully it works for all platforms and versions. I've tested the most basic ones.

Flutter 2.0 how to change elevatedButtonTheme to look like RaisedButton

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