OnPressed effect on font color - flutter

I'm making simple custom Text button
SizedBox(
height: 40,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: (){},
splashColor: Colors.black12,
child: Center(
child: Text(
'Done',
style: TextStyle(
fontSize: 18, color: Colors.white, fontWeight: FontWeight.w500),
),
),
),
),
)
Now this works great however the effect on onTap event is around the text
How to make that effect to be on the text font only or if that is not possible change color of the text on tapped down and change it back on release

For changing the color of the text when pressed down, you can use the onHighlightChanged event handler of InkWell.
In your class declare a color property:
Color textColor = Colors.white;
And change your InkWell implementation:
InkWell(
onTap: () {},
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onHighlightChanged: (value) {
setState(() {
textColor = value ? Colors.white70 : Colors.white;
});
},
child: Center(
child: Text(
Done',
style: TextStyle(
fontSize: 18,
color: textColor,
fontWeight: FontWeight.w500
),
),
),
)
Note:
You have to set the splashColor as transparent
You have to set the highlightColor as transparent

Related

Flutter TextButton is there but not displaying

I've been updating a Flutter app and replaced a FlatButton with the new TextButton. But now the button doesn't display on the Card. I can click it and it works, and if I long press you can see the button and it's caption.
The card widget code is below.
Card otherSwapCard(
List<FSRows?> data, int index, context, Function circularprogress) {
String? shiftDate = formatJsonDate(data[index]!.shiftDate!, 'dd/MM/yyyy');
//calculate time value string
String shiftTimes =
'${formatJsonTime24To12(data[index]!.startTime!)} - ${formatJsonTime24To12(data[index]!.finishTime!)}';
return Card(
color: Colors.white,
elevation: 3,
margin: EdgeInsets.fromLTRB(16, 4, 16, 12),
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 2.0,
color: kMainColor40,
),
),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Expanded(
flex: 72,
child: Column(
children: <Widget>[
DataKeyRow(dkLabel: 'Job:', dkValue: data[index]!.jobName!),
SizedBox(height: 2),
DataKeyRow(dkLabel: 'Date:', dkValue: shiftDate!),
SizedBox(height: 2),
DataKeyRow(
dkLabel: 'Time:', dkValue: shiftTimes.toLowerCase()),
],
),
),
Expanded(
flex: 28,
child: Center(
child: TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
child: Text('Fill', style: TextStyle(color: Colors.white)),
onPressed: () { },
),
),
),
],
),
),
),
);
}
Card, TextButton, and Text three are in white color. So trying changing the color of the Button or Text.
To change the TextButton color
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: Colors.black, // Background Color
),
child: const Text(
'Text Button ',
style: TextStyle(fontSize: 24),
),
)
The backgroundColor will change the TextButton background color to black and the primary will change the font color to white, you can customize it accordingly.
Both your Card color and TextButton Text color are White, you just need to change one of them.
I copied your code and change the color and everything went fine.
Card(
color: Colors.white,
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
child: Text('Fill', style: TextStyle(color: Colors.**white**)),
onPressed: () { },
),
Card and TextButton both are in white color , so try changing your code.
Change
child: Text('Fill', style: TextStyle(color: Colors.white)),
to
child: Text('Fill', style: TextStyle(color: Colors.black)),

How to add presence animation to button in Flutter?

I have a TextButton. I want to add an animation to this TextButton. I want to add presence animation. So instead of suddenly existing without animation, it will slowly exist. How can I do that?
Codes:
TextButton(
child: Text('Example text button'),
onPressed: () {
print('Clicked');
}
)
You can use these package argon_buttons to add animation to a button.
flutter_spinkit
ArgonButton(
height: 50,
width: 350,
borderRadius: 5.0,
color: Color(0xFF7866FE),
child: Text(
"Continue",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w700
),
),
loader: Container(
padding: EdgeInsets.all(10),
child: SpinKitRotatingCircle(
color: Colors.white,
// size: loaderWidth ,
),
),
onTap: (startLoading, stopLoading, btnState) {
},
)

Google font color is ignored

I have an OutlineButton where I try to use a google font for the text using the google-fonts dependency. The code looks like that:
OutlinedButton(
child: const Text('Next'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith((states) {
return colorPrimaryButton;
}),
textStyle: MaterialStateProperty.resolveWith((states) {
return GoogleFonts.inter(textStyle: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white));
})
),
onPressed: () {
// do something
}),
Although I assigned white as the text color, the text appears blue-ish.
What am I missing here? Why wouldn't the "Next" text appear in white?
Don't need to change button textStyle just change child textSytle
OutlinedButton(
child: Text(
'Next',
style: GoogleFonts.inter(
textStyle: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith((states) {
return Colors.indigo;
}),
),
onPressed: () {
// do something
})
Preview

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:

Material Button disabledColor not showing entered color in Flutter

I'm trying to display different color when Material button is disabled.
I'm adding property disabledColor and disabledTextColor.
However, disabledTextColor showing exact color entered but disabledColor not showing any color.
Here is my code
disabledColor:Colors.grey, //not working for background color of button
disabledTextColor:Colors.black, // working for text color of button
MaterialButton(
padding: EdgeInsets.all(10.0),
disabledElevation: 1,
disabledColor: Colors.black45,
disabledTextColor: Colors.white70,
color:Colors.indigo,
textColor: Colors.white,
child: Text("Verify",style: TextStyle(
fontSize: 18.0,
),),
onPressed: null,
),
I expect the output should display grey as background color and black as text color.
Looks like there is a bug on MaterialButton widget, the disabledColor variable is not being used, try using RawMaterialButton.
bool enabled = false;
...
RawMaterialButton(
padding: EdgeInsets.all(10.0),
disabledElevation: 1,
fillColor: enabled ? Colors.indigo : Colors.black45,
textStyle: TextStyle(color: enabled ? Colors.white : Colors.white70),
child: Text(
"Verify",
style: TextStyle(
fontSize: 18.0,
),
),
onPressed: enabled ? () {} : null,
),
Use this scheme
inputDecorationTheme: InputDecorationTheme(
iconColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.focused)) {
return Colors.green;
}
if (states.contains(MaterialState.error)) {
return Colors.red;
}
return Colors.green;
}
),
)