TextButton's text color not changing - flutter

I added this theme for my TextButton.
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0))),
textStyle: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed) || states.contains(MaterialState.hovered))
return TextStyles.headline3.copyWith(
color: AppColors.dustyOrange,
);
return TextStyles.headline3;
},
),
side: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed) || states.contains(MaterialState.hovered))
return BorderSide(
width: 1,
color: AppColors.dustyOrange,
);
return BorderSide(width: 1, color: AppColors.white.withOpacity(0.2));
},
),
),
)
and here is my TextButton:
TextButton(
onPressed: () => widget.onTap(),
child: Text(widget.title, style: theme?.textTheme.headline3),
style: theme?.textButtonTheme.style!.copyWith(
padding: MaterialStateProperty.all(EdgeInsets.symmetric(vertical: 15, horizontal: 20)),
)
)
The problem is that when the button pressed, the style of the button is changing, but the color is not.

There are generally three issue in your code. First, you don't need to assign style to the Text because TextButton itself has a style property. Second, you need to create a copy of TextStyle before you return it. And last, the color property needs to be changed to the foreground because the later gets precedence when there's a conflict between color and foreground.
Here's the minimal reproducible code:
#override
Widget build(BuildContext context) {
return Material(
body: Center(
child: Theme(
data: Theme.of(context).copyWith(
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0))),
textStyle: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
final headline3 = Theme.of(context).textTheme.headline3;
if (states.contains(MaterialState.pressed) || states.contains(MaterialState.hovered)) {
final textStyle = headline3.copyWith(foreground: Paint()..color = Colors.orange);
return textStyle;
}
return headline3;
},
),
),
),
),
child: TextButton(
onPressed: () {},
style: Theme.of(context).textButtonTheme.style,
child: Text('Button'),
),
),
),
);
}
Output of it

Related

Why is ButtonStyle animation slowly?

I have outlined buttons with buttonstyle animation, but this animation is slowl or none because I need tap on button few milliseconds (cca 0.5s)
OutlinedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Rekon()),
);
},
style: buttonStyle,
child: Text(
"TEXT",
style: GoogleFonts.lilitaOne(
fontSize: textSizeCalculator
.calculateTextSize(context),
),
),
),
),
ButtonStyle
ButtonStyle buttonStyle = ButtonStyle(
foregroundColor:
MaterialStateProperty.resolveWith<Color>((Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) {
return const Color.fromARGB(255, 251, 255, 0);
}
return Colors.white;
}),
overlayColor:
MaterialStateColor.resolveWith((states) => const Color(0xFF011230)),
minimumSize: const MaterialStatePropertyAll(Size.fromHeight(60)),
padding: const MaterialStatePropertyAll(EdgeInsets.all(5)),
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
),
side: MaterialStateProperty.resolveWith<BorderSide>(
(Set<MaterialState> states) {
final Color color = states.contains(MaterialState.pressed)
? const Color.fromARGB(255, 255, 0, 0)
: Colors.lightBlueAccent;
return BorderSide(color: color, width: 2.5);
}),
);
but this animation is slow. I have to tap the button for a few milliseconds for the animation to show. How to fix it, or what to use for the requested animation?
Thank you very much in advance for the information.

How to give Rounded Border to Elevated Button in flutter?

As RaisedButton and OutlineButton are deprecated, the flutter team introduces a new ElevatedButton. But I don't know how to make ElevatedButton's border rounded like the below image.
ElevatedButton(
onPressed: () {},
child: Text('Testing'),
),
You could do something like this:
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
),
child: Text(' Elevated Button')
)
ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(color: Colors.green)),
),
),
onPressed: () {},
child: Text('test'),
)
This is how you can use new Ele Button
ElevatedButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.resolveWith<OutlinedBorder>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return ContinuousRectangleBorder(borderRadius: BorderRadius.circular(10));
}
return ContinuousRectangleBorder(borderRadius: BorderRadius.circular(10)); //CHoose any shape you want
},
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return selectedPrimaryColor;
},
),
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.black;
}
return selectedPrimaryColor;
},
),
),
),

How can I change the background color of Elevated Button in Flutter from function?

I am new to Flutter, and I started Flutter last week. And now I want to make a simple Xylophone application. I created the UI successfully and made a function playSound(int soundNumber), but when I call this function for playing sound, it gives me this error.
The following _TypeError was thrown building Body(dirty, state: _BodyState#051c2):
type '_MaterialStatePropertyAll' is not a subtype of type 'MaterialStateProperty<Color?>?'
Here's the code I wrote for the playSound(int soundNumber) function.
void playSound(int soundNumber) {
final player = AudioCache();
player.play('note$soundNumber.wav');
}
Expanded buildPlayButton({MaterialStateProperty color, int soundNumber}) {
return Expanded(
child: ElevatedButton(
onPressed: () {
playSound(soundNumber);
},
style: ButtonStyle(
backgroundColor: color,
),
),
);
}
Here is the point where I am calling this function.
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
buildPlayButton(color: MaterialStateProperty.all(Colors.red), soundNumber: 1),
buildPlayButton(color: MaterialStateProperty.all(Colors.orangeAccent), soundNumber: 2),
buildPlayButton(color: MaterialStateProperty.all(Colors.yellow), soundNumber: 3),
buildPlayButton(color: MaterialStateProperty.all(Colors.indigo), soundNumber: 4),
buildPlayButton(color: MaterialStateProperty.all(Colors.blue), soundNumber: 5),
buildPlayButton(color: MaterialStateProperty.all(Colors.lightGreenAccent), soundNumber: 6),
buildPlayButton(color: MaterialStateProperty.all(Colors.green), soundNumber: 7),
],
);
}
How can I call this function, because it gives me the above-mentioned error?
You can style ElevatedButton by using the styleFrom static method or the ButtonStyle class. The first one is more convenient than the second one.
Using styleFrom to style an ElevatedButton:
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ElevatedButton.styleFrom({
Color primary, // set the background color
Color onPrimary,
Color onSurface,
Color shadowColor,
double elevation,
TextStyle textStyle,
EdgeInsetsGeometry padding,
Size minimumSize,
BorderSide side,
OutlinedBorder shape,
MouseCursor enabledMouseCursor,
MouseCursor disabledMouseCursor,
VisualDensity visualDensity,
MaterialTapTargetSize tapTargetSize,
Duration animationDuration,
bool enableFeedback
}),
),
Example:
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.purple,
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
textStyle: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold)),
),
Using ButtonStyle to style an ElevatedButton:
style: ButtonStyle({
MaterialStateProperty<TextStyle> textStyle,
MaterialStateProperty<Color> backgroundColor,
MaterialStateProperty<Color> foregroundColor,
MaterialStateProperty<Color> overlayColor,
MaterialStateProperty<Color> shadowColor,
MaterialStateProperty<double> elevation,
MaterialStateProperty<EdgeInsetsGeometry> padding,
MaterialStateProperty<Size> minimumSize,
MaterialStateProperty<BorderSide> side,
MaterialStateProperty<OutlinedBorder> shape,
MaterialStateProperty<MouseCursor> mouseCursor,
VisualDensity visualDensity,
MaterialTapTargetSize tapTargetSize,
Duration animationDuration,
bool enableFeedback
})
Example
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
padding: MaterialStateProperty.all(EdgeInsets.all(50)),
textStyle: MaterialStateProperty.all(TextStyle(fontSize: 30))),
),
Pass color as parameter and use MaterialStateProperty.all<Color>(color) to specify the color.
buildPlayButton(color: Colors.red, soundNumber: 1)
Expanded buildPlayButton({Color color, int soundNumber}){
return Expanded(
child: ElevatedButton(
onPressed: () {
playSound(soundNumber);
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(color),
),
),
);}
Sample button
In general
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red, // background
onPrimary: Colors.yellow, // foreground
),
onPressed: () {},
child: Text('ElevatedButton with custom foreground/background'),
)
Sample button
Reference:
ElevatedButton class
ElevatedButton(onPressed: resetHandler,
child: Text("button"),
style: ElevatedButton.styleFrom(primary: Colors.amber),),
Just use MaterialStateProperty.all(**YOUR COLOR**):
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),)
),
or like this:
Just use ElevatedButton.styleFrom(primary: **YOUR COLOR**):
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ElevatedButton.styleFrom(primary: Colors.red),
)
You have three options to change the background color:
ElevatedButton.styleFrom:
If you just want to change the background color and foreground color irrespective of the states then you can do as given below.
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red, // Background
onPrimary: Colors.white, // Foreground
),
onPressed: () { },
child: Text('custom foreground/background'),
)
MaterialStateProperty.all:
to override a ElevatedButtons default background(text/icon) color for all states.
ElevatedButton(style:
ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
onPressed: () { },
child: Text('custom foreground/background'),
));
MaterialStateProperty.resolveWith:
By default, the elevated button inherits a blue color. We can tweak the default style using the style parameter and ButtonStyle class.
Button has different states such as pressed, disabled, hovered, etc. You can change the style for each state. In the below snippet, the default color of the button changes to green when it is pressed.
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Colors.green;
return null; // Use the component's default.
},
),
),
)
Suppose we need to change Elevated Button Background color then? Elevated Button has a style Property And style property need ButtonStyle(). ButtonStyle has backgroundColor property which requires MaterialStateProperty. You can simply assign background color by MaterialStateProperty.all(Colors.green). Let’s explore examples of Background color of Elevated Button in Flutter.
ElevatedButton(
onPressed: () {
print('Button Pressed');
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.green),
),
child: Text('Send'),
),
Screenshot:
Code:
class _MyState extends State<MyPage> {
bool _flag = true;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => setState(() => _flag = !_flag),
child: Text(_flag ? 'Red' : 'Green'),
style: ElevatedButton.styleFrom(
backgroundColor: _flag ? Colors.red : Colors.teal, // This is what you need!
),
),
),
);
}
}
The current best answer with the example of ElevatedButton.styleFrom is outdated. As of Flutter v3.1.0, the primary parameter is deprecated.
Color? primary // Use foregroundColor instead. This feature was deprecated after v3.1.0.
Instead, use the backgroundColor parameter:
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: const Colors.red,
),
onPressed: () {},
child: const Text('Test'),
)
You can simply use this code inside the ElevatedButton
style: ElevatedButton.styleFrom(
backgroundColor:Theme.of(context).primaryColor
),
ElevatedButton(
onPressed: (){},
child: Text('comprar'),
style: ElevatedButton.styleFrom(
primary: Theme.of(context).primaryColor
)
)
style: ElevatedButton.styleFrom(primary : Colors.black),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
primary: HexColor(HexColor.primarycolor),
textStyle: TextStyle(fontWeight: FontWeight.bold)),
You need to set the primary property (inside a style) and assign it a color, but be careful, if you haven't set your onPressed() event then the color doesn't change..
Here is an example:
Widget renderMyButton() {
return ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.lightBlue, // Change the color here..
elevation: 0,
// ...
),
onPressed: () {}, // This line is important to change the ElevatedButton color..
child: Container()
);
}
style: ButtonStyle({
MaterialStateProperty.all(backgroundColor),
),
Similarly, you can add MaterialStateProperty.all(<Value here>) to most properties of elevated button(elevation, padding, border etc).
Make sure to add onPressed: () {},
Otherwise the color will be gray.
If you want to change the elevated button background color and outline color also with the shape of the circle, then checkout this code:
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
side: BorderSide(
width: 1,
color: primaryBlue),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
20,
))),
onPressed: () {},
child: Text(
'Use camera',
style: t3b,
),
),
This code will look like this:

Elevated Button height not increasing

Since Raised button is deprecated I replaced with Elevated Button. But I can't increase Elevated button's height.
class ZuzuButton extends StatelessWidget {
final Function onTapped;
final String name;
final double height;
final TextStyle textStyle;
final double radius;
final List<BoxShadow> shadow;
final Color color;
final bool enable;
ZuzuButton({this.onTapped,#required this.name,
this.height,this.textStyle,this.radius,this.shadow,this.color,this.enable=true});
#override
Widget build(BuildContext context) {
return Container(
height: height==0?48.0:height,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(radius!=null?radius:30.0),
border: enable? Border.all(
width: color!=null?0.0:1.0,
color: color!=null?color:Color(0x407F16F0),
):null,
boxShadow: enable?(shadow==null?[
BoxShadow(
color: Color(0x407F16F0),
offset: Offset(0.0, 8.0),
spreadRadius: 0,
blurRadius: 20,
),
]:shadow):null,
),
child: ElevatedButton(
child: Container(
child: Center(
child: Text(name,style: textStyle!=null?textStyle:null,),
),
height: height==0?48.0:height,
),
onPressed: enable?onTapped:null,
style: ButtonStyle(
elevation: MaterialStateProperty.resolveWith<double>(
(Set<MaterialState> states) {
return 0.0;
},
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Color(0xffF7E86C);
return enable?(color!=null?color:null):Color(0xffDBD9D2); // Use the component's default.
},
),
textStyle: MaterialStateProperty.resolveWith<TextStyle>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return ZuzuTopography.FF2_Button_Bold.copyWith(color: Colors.black);
return ZuzuTopography.FF2_Button_Bold.copyWith(color: Colors.white); // Use the component's default.
},
),
shape: MaterialStateProperty.resolveWith<OutlinedBorder>(
(Set<MaterialState> states) {
// if (states.contains(MaterialState.pressed))
// return radius!=null? RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(radius),
// ):null;
return RoundedRectangleBorder(
borderRadius: BorderRadius.circular(radius!=null?radius:30.0),
); // Use the component's default.
},
),
),
),
);
}
}
My output.
How to make this button occupy its container height? I searched internet for solutions but could not found any solutions. Any suggestions in my code? Is there any alternative for Raised Button other than Elevated Button.
I just started using Elevated Button. For me I just change the height using this:
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
minimumSize: Size(width, height) // put the width and height you want
),
child: Text("NEXT"),
)
You can use ConstrainedBox for doing the same. Please refer below code for the reference.
ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 300, height: 200),
child: ElevatedButton(
child: Text('300 x 200'),
onPressed: () {},
),
),
Use SizeBox with width and height parameters.
SizedBox(
width: double.infinity,
height: 55.0,
child: ElevatedButton(
),
);
You can simply use fixedSize(width, height). Here is a sample
ElevatedButton(
onPressed: () {},
child: Text(
'submit',
),
style: ElevatedButton.styleFrom(
fixedSize: Size(90, 15),
primary: Colors.deepOrange,
),
)
You can use minimumSize property of an elevated button instead of SizedBox:
ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 0,
minimumSize: Size(100, 48), // Size(width, height)
backgroundColor: AppColors.primary,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8))),
child: Text("Button Text", style: textTheme.button),
onPressed: (){},
),

How to I change the disabled color of an ElevatedButton

I am using Flutter ElevatedButton, which is recommended over RaisedButton in the docs.
In raised button, you could specify and disabledColor. In ElevatedButton, I can not.
How can I stylize what the ElevatedButton looks like when it is disabled?
You can copy paste run full code below
You can use ButtonStyle and check states.contains(MaterialState.disabled) return color you need
In demo code, disabled color is green
code snippet
ElevatedButton(
onPressed: null,
child: Text('Submit disable'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.5);
else if (states.contains(MaterialState.disabled))
return Colors.green;
return null; // Use the component's default.
},
),
),
),
working demo
full code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: null,
child: Text('Submit disable'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.5);
else if (states.contains(MaterialState.disabled))
return Colors.green;
return null; // Use the component's default.
},
),
),
),
ElevatedButton(
onPressed: () {
print("clicked");
},
child: Text('Submit enable'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.5);
else if (states.contains(MaterialState.disabled))
return Colors.green;
return null; // Use the component's default./ Use the component's default.
},
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
A simple solution would be to use onSurface property:
ElevatedButton(
onPressed: null,
style: ElevatedButton.styleFrom(
onSurface: Colors.brown,
),
child: Text('Button'),
)
use onSurface in ElevatedButton.styleFrom
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white,
onSurface: Colors.green,
),
child: Text("next"),
onPressed:null
)
Try This:
style: ElevatedButton.styleFrom(
disabledBackgroundColor:
Theme.of(context).primaryColor.withOpacity(.8), // Background Color
disabledForegroundColor: Colors.white70, //Text Color
),
Use button's style property. It contains backgroundColor of MaterialStateProperty<Color>. There is a constant MaterialState.disabled. I think this is what you need.
https://api.flutter.dev/flutter/material/MaterialStateProperty-class.html
I created some code to handle disabledText and disabledColor for both FlatButton and RaisedButton with the new TextButton and ElevatedButton widgets.
This gist is here (I couldn't get dartpad to recognize the gist... but you can copy and paste it there directly and it works... it just won't link)
https://gist.github.com/wterrill/7942b4bf5d74a8b2ace952ebf213fe5d
And here's the code itself if you want to copy and paste from here:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final bool disableButton = true; // <-- Change this to see buttons disabled
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
child: Text("FlatButton"),
onPressed: disableButton
? null
: () {
print("FlatButton normal");
},
color: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColor: Colors.grey,
disabledTextColor: Colors.pink),
SizedBox(height: 25),
FlatButtonX(
childx: Text("TextButton"),
onPressedx: disableButton
? null
: () {
print("FlatButtonX (TextButton)");
},
colorx: Colors.green,
textColorx: Colors.black,
shapex: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColorx: Colors.grey,
disabledTextColorx: Colors.pink),
SizedBox(height: 100),
RaisedButton(
child: Text('RaisedButton'),
color: Colors.green,
textColor: Colors.black,
onPressed: disableButton
? null
: () {
print("RaisedButton normal");
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColor: Colors.grey,
disabledTextColor: Colors.pink,
),
SizedBox(height: 25),
RaisedButtonX(
childx: Text('ElevatedButton'),
colorx: Colors.green,
textColorx: Colors.black,
onPressedx:disableButton
? null
: () {
print("RaisedButtonX (ElevatedButton)");
},
shapex: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColorx: Colors.grey,
disabledTextColorx: Colors.pink,
)
],
),
),
),
);
}
}
Widget FlatButtonX(
{Color colorx,
#required Widget childx,
RoundedRectangleBorder shapex,
#required Function onPressedx,
Key keyx,
Color disabledColorx,
Color disabledTextColorx,
Color textColorx}) {
if (disabledTextColorx == null && textColorx == null) {
disabledTextColorx = colorx;
}
if (textColorx == null) {
textColorx = colorx;
}
return TextButton(
key: keyx,
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
// text color
(Set<MaterialState> states) => states.contains(MaterialState.disabled)
? disabledTextColorx
: textColorx,
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
// background color this is color:
(Set<MaterialState> states) =>
states.contains(MaterialState.disabled) ? disabledColorx : colorx,
),
shape: MaterialStateProperty.all(shapex),
),
onPressed: onPressedx as void Function(),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0), child: childx));
}
Widget RaisedButtonX(
{Color colorx,
#required Widget childx,
RoundedRectangleBorder shapex,
#required Function onPressedx,
Key keyx,
Color disabledColorx,
Color disabledTextColorx,
Color textColorx}) {
if (disabledTextColorx == null && textColorx == null) {
disabledTextColorx = colorx;
}
if (textColorx == null) {
textColorx = colorx;
}
return ElevatedButton(
key: keyx,
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
// text color
(Set<MaterialState> states) => states.contains(MaterialState.disabled)
? disabledTextColorx
: textColorx,
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
// background color this is color:
(Set<MaterialState> states) =>
states.contains(MaterialState.disabled) ? disabledColorx : colorx,
),
shape: MaterialStateProperty.all(shapex),
),
onPressed: onPressedx as void Function(),
child: childx);
}
Because the original disabled color has opacity you can simply wrap it in a container and give the container the color you want. It's a bit hacky but it works and is fast done. Especialy if you have many buttons with different colors.
The cleanest way would be a custom button widget where you can handover the parameters you need.
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(const Radius.circular(radius)),
color: containerColor,
),
width: containerWidth,
height: containerHeight,
child: ElevatedButton(
onPressed: bool ? onPressed : null,
child: Text(
buttonText,
),
),
),
Make all variables nullable which are not allways needed.