Home > database >  Dart / Flutter The argument type 'Color' can't be assigned to the parameter type 
Dart / Flutter The argument type 'Color' can't be assigned to the parameter type 

Time:06-17

I am trying to make the a Button in flutter reusable as it is pretty much the same except for the Color of the Button. As far as I understand from a number search results, the datatype for colors is Color. But when I do the following it gives me an error saying The argument type 'Color' can't be assigned to the parameter type 'int'.

class CustomButton extends StatelessWidget {
  final Color buttonColor;
  final Color textColor;

  const CustomButton({
    Key? key,
    required this.buttonColor,
    required this.textColor,
  }) : super(key: key);

And I mentioned it like the following:

style: TextStyle(
   fontSize: 16,
   fontWeight: FontWeight.bold,
   color: Color(textColor)),

Thanks in advance.

CodePudding user response:

The property color in the TextStyle accepts a value of type Color which is a dart object, not the integer value of the color code.

So you can keep textColor with type Color and use it like the following:

style: TextStyle(
    ...
    color: textColor // textColor is a variable of type Color
    ...
),

Or change the type of the textColor variable to be int containing the value (Color Code) of the color, example: 0xFFFF0000, and then use it like the following:

style: TextStyle(
    ...
    color: Color(textColor) // textColor is a variable of type int containing the color code
    ...
),
  • Related