Dart, Flutter; Hot to get color from MaterialStateProperty
307
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.
Author by
Chris
Updated on December 30, 2022Comments
-
Chris over 1 year
Some values in a
Theme
object must be provided asMaterialStateProperty<T>
, e.g.backgroundColor: MaterialStateProperty.all<Color>(Colors.white)
. How can the property of typeT
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'*
-
Moacir Schmidt about 2 yearselevatedButtonTheme.style!.backgroundColor!.resolve({MaterialState.pressed}))