Open bernaferrari opened 5 years ago
I think you can generate a Theme of the current Theme with Theme.of(context).copyWith();
The issue with copyWith
is that you need to add it manually every time you call ThemeData.from()
. ThemeData.from()
does a lot of magic, but it doesn't merge with your current theme. It gets worse as code grows.
CC @darrenaustin
maybe we should consider something similar to textTheme apply method ?
As noted in https://github.com/flutter/flutter/pull/43825#issuecomment-639685532, there are some open questions implicit in this:
@HansMuller one reason for merging can be that I have two static themes defined in my app, a light
and a dark
one. But there are some settings in both that are common, not depending on the light-dark distinction. It would be nice to have them in a third theme, common
, and simply build both light
and dark
on this common theme. Much like object inheritance, really: what I override in a descendant theme, gets overridden and what I don't touch comes from the base theme.
copyWith()
does part of this, but at the variable level. I can replace eg. a complete textTheme
but I can't specify textTheme.caption
in my common
theme end expect the light
theme to only override textTheme.caption
.
But that would a deeper change, it would practically require a merge()
for each individual theme element, maybe even making them implement the same interface, and the upper level merge calling all lower lever merge functions...
@HansMuller one reason for merging can be that I have two static themes defined in my app, a
light
and adark
one. But there are some settings in both that are common, not depending on the light-dark distinction. It would be nice to have them in a third theme,common
, and simply build bothlight
anddark
on this common theme. Much like object inheritance, really: what I override in a descendant theme, gets overridden and what I don't touch comes from the base theme.
copyWith()
does part of this, but at the variable level. I can replace eg. a completetextTheme
but I can't specifytextTheme.caption
in mycommon
theme end expect thelight
theme to only overridetextTheme.caption
.But that would a deeper change, it would practically require a
merge()
for each individual theme element, maybe even making them implement the same interface, and the upper level merge calling all lower lever merge functions...
This is exactly what I'm trying to do.
How does someone put together ThemeData.from() with an existing theme? It works nice on the root of a project, but if you have it anywhere else things start to break and boilerplate starts adding fast.
When I use
ThemeData.from()
, I loose all the shapes, elevations, radius and etc. It is great in the main() class, but for the others..? I wish I could just apply theColorScheme
and it only changed the colors, not the full theme. Instead, currently, there is a lot of manual work involved.