Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

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

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

Short answer

ButtonStyle and similar properties take a per-state value, not a plain color. Wrap one color for every state in WidgetStatePropertyAll, return different colors with WidgetStateProperty.resolveWith, or skip ButtonStyle and use the button's styleFrom shortcut. MaterialStateProperty is the old name of the same class.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the line the analyzer points at. Note which property gets the color (backgroundColor, foregroundColor, fillColor): that property wants a state property.

    flutter analyze
  2. 2Match what you see, then jump to its fix

    1. 1The error sits on backgroundColor: or foregroundColor: inside style: ButtonStyle(...)A raw color is passed into ButtonStyleFix
    2. 2You only need a background and a text colorYou want one simple color, not per-state logicFix
    3. 3The same error on fillColor: of a Checkbox, or on a property you want to change when disabledDifferent colors per state on a checkbox or buttonFix
    4. 4Code or an example still says MaterialStateProperty or MaterialState.pressedOld MaterialState names in codeFix

What does the error look like?

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

Cause 1 of 4

A raw color is passed into ButtonStyle

1Check

In ButtonStyle these properties resolve a color per state (pressed, hovered, focused, disabled). A Colors.red value is not one.

ElevatedButton(
  style: ButtonStyle(backgroundColor: Colors.red), // error
  onPressed: () {},
  child: const Text('Save'),
)

2Fix

For one color in every state, wrap it in WidgetStatePropertyAll.

ElevatedButton(
  style: ButtonStyle(
    backgroundColor: WidgetStatePropertyAll<Color>(Colors.red),
  ),
  onPressed: () {},
  child: const Text('Save'),
)

3Verify

The analyzer error is gone and the button shows the color.

flutter analyze

Confirmed by api.flutter.dev

Did this fix it?

Cause 2 of 4

You want one simple color, not per-state logic

1Check

styleFrom takes plain colors and builds the state properties for you.

grep -rn "ButtonStyle(" lib/

2Fix

Use the styleFrom shortcut of the button class.

ElevatedButton(
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.red,
    foregroundColor: Colors.white,
  ),
  onPressed: () {},
  child: const Text('Save'),
)

3Verify

Run the app: the button shows both colors.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 3 of 4

Different colors per state on a checkbox or button

1Check

Checkbox.fillColor takes a WidgetStateProperty<Color?>, resolved for the selected, hovered, focused and disabled states.

grep -rn "fillColor:" lib/

2Fix

Return a color from the set of states with WidgetStateProperty.resolveWith. Returning null falls back to the default.

Checkbox(
  value: checked,
  onChanged: (v) => setState(() => checked = v!),
  fillColor: WidgetStateProperty.resolveWith<Color?>(
    (Set<WidgetState> states) {
      if (states.contains(WidgetState.disabled)) {
        return Colors.grey;
      }
      return Colors.orange;
    },
  ),
)

3Verify

Toggle enabled and disabled and compare the colors.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 4 of 4

Old MaterialState names in code

1Check

Since Flutter 3.22 these were moved and renamed to WidgetState. It is the same behavior with new names.

grep -rn "MaterialState" lib/

2Fix

Replace MaterialState with WidgetState, MaterialStateProperty with WidgetStateProperty, and MaterialStatePropertyAll with WidgetStatePropertyAll.

grep -rln "MaterialStateProperty" lib/ | xargs sed -i '' 's/MaterialStateProperty/WidgetStateProperty/g'

3Verify

Search again: no MaterialState left apart from the two input border classes.

grep -rn "MaterialState" lib/

Confirmed by docs.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Prefer styleFrom for simple colors.
  • Use the Flutter docs for your installed version, not an old tutorial.
  • Read the breaking-changes page after each Flutter upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.