Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color>

Tags:

flutter

dart

I have a question related with the new ElevatedButtonThemeData widget, basically I want to set the background color for all ElevatedButtons in my app, I'm struggling trying to set it up in the ThemeData definition by doing:

      theme: ThemeData(
        ...
        elevatedButtonTheme: ElevatedButtonThemeData(
            style: ButtonStyle(backgroundColor: Colors.red)), // Here Im having the error
        ...
        ),
      ),

Error:

The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color?>?'.dartargument_type_not_assignable)
like image 846
mcarlomagno Avatar asked Jun 02 '21 23:06

mcarlomagno


People also ask

What is MaterialStateProperty?

Material state properties represent values that depend on a widget's material "state". The state is encoded as a set of MaterialState values, like MaterialState. focused, MaterialState. hovered, MaterialState. pressed.

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.


2 Answers

After reading the documentation I found the way to set the color.

  theme: ThemeData(
    ...
    elevatedButtonTheme: ElevatedButtonThemeData(
        style: ButtonStyle(backgroundColor: MaterialStateProperty.all<Color>(Colors.red))), // Here Im having the error
    ...
    ),
  ),
like image 65
mcarlomagno Avatar answered Oct 08 '22 19:10

mcarlomagno


Here is a code snippet that shows how I have styled a text button, by using material state properties.

You can see how can you add different types of values:

TextButton(style: ButtonStyle(
      padding: MaterialStateProperty.all(const EdgeInsets.all(0)),
      elevation: MaterialStateProperty.all(8),
      shape: MaterialStateProperty.all(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(50))),
      backgroundColor: MaterialStateProperty.all(Colors.blue),
      shadowColor: MaterialStateProperty.all(
          Theme.of(context).colorScheme.onSurface),
    ),),

I hope this gives you an idea.

like image 22
Rashid Wassan Avatar answered Oct 08 '22 17:10

Rashid Wassan