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)
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.
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.
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
...
),
),
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With