I want to create a design system for my new app.
child: ElevatedButton.icon(
icon: Icon(Icons.power_settings_new),
onPressed: () => context.go('/login'),
label: const Text('Log out'),
),
It’s ok but in this scenario it should have a red color accent. This goal is easily achievable by use of foregroundColor property. By use of this technique there is no need to worry about icon’s color, text’s color, ripple effect color.
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.red,
),
icon: Icon(Icons.power_settings_new),
onPressed: () => context.go('/login'),
label: const Text('Log out'),
),
However I don’t want to specify this color every time. I’d prefer to make something like this and I’d like to avoid writing anny wrappers for buttons.
child: ElevatedButton.error( // Color accent is red
icon: Icon(Icons.power_settings_new),
onPressed: () => context.go('/login'),
label: const Text('Log out'),
),
Question 1: Is it event possible?
Question 2: How icon, text and ripple effects know that they need use a foreground color to render themselves?
3
Answers
I would look into Extensions (https://dart.dev/guides/language/extension-methods)
Wrap a widget with
Theme
:Then go for a more specific style as needed:
Extension without parameter
You can define every instance for
error
,success
, by just changing the color.And use it as
Extension with parameter
You can make it more generic by using the parameter and pass in the required color
And use it as
Extra: if you further want to use the extension with all parameters you should define every desired extension with the preferred name.
And use it as