Chris
Chris

Reputation: 71

Dart, Flutter; How to get color from MaterialStateProperty

Some values in a Theme object must be provided as MaterialStateProperty<T>, e.g. backgroundColor: MaterialStateProperty.all<Color>(Colors.white). How can the property of type T be accessed in a Flutter widget?

E.g. the following line produces an (obvious) error:

Container(color: Theme.of(context).elevatedButtonTheme.style?.backgroundColor)
Error: *The argument type 'MaterialStateProperty<Color?>?' can't be assigned to the parameter type 'Color'*

Upvotes: 3

Views: 1675

Answers (1)

Wiktor Metelski
Wiktor Metelski

Reputation: 111

You should try

Container(color: Theme.of(context).elevatedButtonTheme.style!.backgroundColor!.resolve(Set.of[MaterialState.pressed]))

But you make to have sure that there is backgroundColor for this theme, otherwise you will get "Unexpected null value" exception.

Upvotes: 4

Related Questions