flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.22k stars 27.5k forks source link

ThemeData.from is good, but does not merge with existing theme #43823

Open bernaferrari opened 5 years ago

bernaferrari commented 5 years ago

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.

image

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 the ColorScheme and it only changed the colors, not the full theme. Instead, currently, there is a lot of manual work involved.

elMuso commented 5 years ago

I think you can generate a Theme of the current Theme with Theme.of(context).copyWith();

bernaferrari commented 5 years ago

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.

HansMuller commented 5 years ago

CC @darrenaustin

bitsydarel commented 4 years ago

maybe we should consider something similar to textTheme apply method ?

HansMuller commented 4 years ago

As noted in https://github.com/flutter/flutter/pull/43825#issuecomment-639685532, there are some open questions implicit in this:

deakjahn commented 3 years ago

@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...

JavonneM commented 2 years ago

@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...

This is exactly what I'm trying to do.