Add custom property to ThemeData in Flutter Add custom property to ThemeData in Flutter dart dart

Add custom property to ThemeData in Flutter


Sadly, it appears that you simply can't - and the Flutter team doesn't seem to be interested in adding this, given their suggestion.

I think this is a major flaw, because we can't benefit from Theme.of(context) to automatically update all of our Widget that consume this ThemeData.

While some may say that you can use extensions to add new properties, you effectively won't know how to differ between multiple ThemeData (unless you can use some properties like Brightness, but I think this is just too hacky and not reliable to do so).

The alternative is to create another InheritedWidget (just like they said in the issue mentioned above) to handle your custom theme properties.


Instead of adding custom property, we can extend ThemeData by extension function. For example, if we need a custom property for color, we can add extension function on ColorScheme. Color dependencies are now moved to Themedata.

// checking brightness to support dynamic themeingextension CustomColorSchemeX on ColorScheme {  Color get smallBoxColor1 =>      brightness == Brightness.light ? Colors.blue : Colors.grey[400];}

And then access that property through Theme.of(context)...

                        Container(                          decoration: BoxDecoration(                            border: Border.all(                                color: Theme.of(context)                                    .colorScheme                                    .smallBoxColor1),                        ),