Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The argument type 'Color?' can't be assigned to the parameter type 'MaterialColor?

enter image description here

I want to set the background Colors.yellow[700] in flutter,but when i add symbol "[]" or Colors.yellow.shade600, but i can't set the value for background. It shows error & the error is

The argument type 'MaterialColor' can't be assigned to the parameter type 'Paint'
like image 283
Megantara Avatar asked Jun 11 '21 01:06

Megantara


People also ask

How do you change the color of a material color in flutter?

To turn any color to material, You just follow below, Especially, when we try to give a primary swatch color, It only accepts the material color code. Now, Just create a variable for your custom color and specify your values in it for 50 to 900 for Luminance purpose. Map<int, Color> color ={50:Color.

What is color swatch in flutter?

A color that has a small table of related colors called a "swatch". The table is indexed by values of type T . See also: MaterialColor and MaterialAccentColor, which define material design primary and accent color swatches.

What is materialcolor in material you framework?

In the "Material You" framework there is a class named MaterialColor. This is not stated. All colors in Material classes are of type MaterialColor. The "Colors" class is a Material class that has public elements having the primary colors are names. Each of these elements is a List of type "MaterialColor".

What is the difference between materialcolor and color in Dart?

It too has a class named Color. Because of type safety the two classes MaterialColor and Color are incompatible. Therefore whenever you want to create or reference a color in the Material frame work, use the type MaterialColor rather than the Dart type "Color" And be careful when you read a parameter named color or colors!

Why does colors blueaccent return a null value in flutter?

If you see Colors.blueAccent [100] actually gets a value from map. So if your version of flutter is bellow 2.0 you would get this error since it may return a null value.

Why isn't black and white a color in the material design?

Officially, black and white aren't even colors in the same way that a Tomato isn't officially a vegetable but a fruit. Weird, but true. so that's probably why black and white are treated differently in the Material design spec than the other "colors" that do have swatches.


2 Answers

If you want primarySwatch with Colors.yellow[700] as primaryColor you would have to create your own MaterialColor from color Colors.yellow[700] like this

final Map<int, Color> _yellow700Map = {
  50: Color(0xFFFFD7C2),
  100: Colors.yellow[100],
  200: Colors.yellow[200],
  300: Colors.yellow[300],
  400: Colors.yellow[400],
  500: Colors.yellow[500],
  600: Colors.yellow[600],
  700: Colors.yellow[800],
  800: Colors.yellow[900],
  900: Colors.yellow[700],
};

final MaterialColor _yellow700Swatch =
  MaterialColor(Colors.yellow[700].value, _yellow700Map);

and then add it as primarySwatch: _yellow700Swatch, or if you want only your background to be Colors.yellow[700] you can use canvasColor like this canvasColor: Colors.yellow[700],.

like image 83
Yashawant Avatar answered Oct 10 '22 08:10

Yashawant


Also you can use colorScheme property and set like below :

theme: ThemeData(
    colorScheme: ColorScheme.fromSwatch().copyWith(

      primary: const Colors.yellow[700],
      secondary: const Colors.yellow.shade700,

      // or from RGB

      primary: const Color(0xFF343A40),
      secondary: const Color(0xFFFFC107),

    ),
  ),
like image 30
Mahdi Avatar answered Oct 10 '22 07:10

Mahdi