Description
Problem
When using a Scaffold
widget, any parent DefaultTextStyle
will subsequently be ignored:
DefaultTextStyle.merge(
style: TextStyle(
letterSpacing: -0.5,
),
child: Scaffold(
body: Center(
// The default text style set above is ignored here.
child: Text('Foo'),
),
),
)
Use case
This entirely prevents setting a global default text style (e.g. to adjust the letter spacing for all text widgets in an app) when using Scaffold
at all (which is usually used on every page in a Flutter app).
Cause
The reason this happens is because every Scaffold
inserts a Material
widget and this in turn overrides the DefaultTextStyle
with TextTheme.bodyMedium
:
This is also not documented. Neither the Scaffold
widget mentions this behavior, nor the Material
widget itself. Material.textStyle
only states the following (and not that omitting the parameter will override DefaultTextStyle
):
The typographical style to use for text within this material.
The only mention of this in the docs I could find was in TextTheme.bodyMedium
:
The default text style for Material.
This, however, does not help the problem at all as there is no way to prevent the Material
widget from overriding the DefaultTextStyle
when using a Scaffold
. There is no parameter for passing this. And even if there was one, we would have to manually pass our default text style to every Scaffold
or Material
usage within the app.
To me, this seems to be a fundamental issue.
Proposal
I propose the following:
- The documentation should be fixed.
And any (or a combination) of the following should be implemented:
- A flag on the app's
Theme
/TextTheme
passed toMaterialApp
that disables defaulting tobodyMedium
if the app does not usebodyMedium
. - A flag on the
Scaffold
widget to respect theDefaultTextStyle
. - A way to prevent
Material
from overriding the default text style.