I have this block of code
TextButton(
onPressed: () {},
child: Text('Forgot your password?'),
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.primary,
),
),
Yet I get the error below as I run the flutter code:
../../../flutter/packages/flutter/lib/src/material/text_button.dart:146:22: Context: Found this candidate, but the arguments don't match.
static ButtonStyle styleFrom({
^^^^^^^^^
lib/main.dart:93:19: Error: No named parameter with the name 'primary'.
primary: Theme.of(context).colorScheme.primary,
^^^^^^^
Do I need to check my gradle version or whatever? I am very new to flutter any help is appreciated.
>Solution :
The error you are seeing is because the styleFrom method does not have a primary parameter anymore. As can be seen in the documentation, the constructor for this method has Color arguments for different color attributes.
ButtonStyle styleFrom(
{Color? foregroundColor,
Color? backgroundColor,
Color? disabledForegroundColor,
Color? disabledBackgroundColor,
Color? shadowColor,
Color? surfaceTintColor,
Color? iconColor,
Color? disabledIconColor,
double? elevation,
TextStyle? textStyle,
EdgeInsetsGeometry? padding,
Size? minimumSize,
Size? fixedSize,
Size? maximumSize,
BorderSide? side,
OutlinedBorder? shape,
MouseCursor? enabledMouseCursor,
MouseCursor? disabledMouseCursor,
VisualDensity? visualDensity,
MaterialTapTargetSize? tapTargetSize,
Duration? animationDuration,
bool? enableFeedback,
AlignmentGeometry? alignment,
InteractiveInkFeatureFactory? splashFactory}
)
So, if you want to set the background color, then you need to do the following:
TextButton(
onPressed: () {},
child: Text('Forgot your password?'),
style: TextButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
),
),