0
themeData.copyWith(
    colorScheme: //...
    scaffoldBackgroundColor: //...
    textTheme: const TextTheme(
        headline1: TextStyle(/*...*/),
        subtitle1: TextStyle(/*...*/),
        button: TextStyle(/*...*/),
        bodyText1: TextStyle(/*...*/),
    );

VS

class TextThemeConstatns {
  const TextStyle headline1 = TextStyle(/*...*/);
  const TextStyle subtitle1 = TextStyle(/*...*/);
  const TextStyle button = TextStyle(/*...*/);
  const TextStyle bodyText1 = TextStyle(/*...*/);
}

I'm aware that using TextTheme will give me some out-of-the-box styling for Material widgets (buttons, appbars, ect.) But I'm working on a project that, let's say, isn't following material design, so that point is almost irrelevant.

That's said, is there any advantage of defining my styles in TextTheme rather that a simple constants file? Assuming that I can see many advantages for the latter that are out of the scoop of this question.

A similar frequently asked question is "ThemeData vs constants file". Well for that, the ability to change the theme in any point of time is what cuts the deal in favor of ThemeData. In my case, I don't see any case where text styles needs to be changed.

One point that could be relevant, is accessibility options or system defined font size preferences. Maybe using TextTheme utilize the api in a way that makes my app more compatible with some of those cases? I didn't find anything about that.

1 Answers1

1

At first sight of the title, one can directly answer yes. That the advantage of using TextTheme is that Flutter defines it and that we shouldn't be fighting the framework. Flutter is a framework and it defines how to use it. I love how you partly answered the question with

I'm aware that using TextTheme will give me some out-of-the-box styling for Material widgets (buttons, appbars, etc.) ...

and

A similar frequently asked question is "ThemeData vs constants file". Well for that, the ability to change the theme in any point of time is what cuts the deal in favor of ThemeData.


Flutter gives the developer so much freedom. I mean, it defined TextTheme and still made it possible to use "constants file". That said, the fact that we shouldn't fight the framework doesn't mean you are not free to choose "constants file".

You have clearly explained your scenario. Everyone who reads your usecase can clearly see that, for your particular case, there is no difference between TextTheme and "constants file". As you've pointed that at

... In my case, I don't see any case where text styles needs to be changed.

So maybe one can say no, that there is no advantage of TextTheme over "constants file" in the context of your project.


To comment on your last paragraph:

One point that could be relevant, is accessibility options or system defined font size preferences. Maybe using TextTheme utilize the api in a way that makes my app more compatible with some of those cases? I didn't find anything about that.

There is really nothing about that. The rendering layer is down the Flutter Engine's tree. So Flutter will render accessibility or native APIs the same way for both TextTheme and "constants file". Remember that Flutter paints as it like on a custom canvas inside the platform it is running. So both styles should work as expected.

Obum
  • 1,485
  • 3
  • 7
  • 20