Flutter: Theme not applied to Text Widget

Latest Flutter Release mark body1 has deprecated, so now we can use bodyText2:

Text("My Text", style: Theme.of(context).textTheme.bodyText2)

New params(Right ones are new)

body2 => bodyText1;
body1 => bodyText2;

This is another way, because sometimes it's more convenient to override the default style

Text widget:

If the style argument is null, the text will use the style from the closest enclosing DefaultTextStyle.

  @override
  Widget build(BuildContext context) {
    final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
    TextStyle effectiveTextStyle = style;
    if (style == null || style.inherit)
      effectiveTextStyle = defaultTextStyle.style.merge(style);

So, if you want to override the default style for the Text widget (when you do not pass the style property), you need to use the DefaultTextStyle widget

return new Scaffold(
    appBar: new AppBar(
      title: Text(widget.title),
    ),
    body: Center(
        child: DefaultTextStyle(
          style: Theme.of(context).textTheme.body1.copyWith(color: Colors.yellow), 
          child: Text("hello"))
        )
);

DefaultTextStyle

MaterialApp uses its TextStyle as its DefaultTextStyle to encourage developers to be intentional about their DefaultTextStyle


To theme a Text you need to assign the value to style property

Text("Hello", style: Theme.of(context).textTheme.body1)

Make sure to use the correct context when doing Theme.of(context). You need a context that is a child of your new Theme.

You'll need to do the following :

 Theme(
     child: Builder(
       builder: (context) {
         return Text("Hello", style: Theme.of(context).textTheme.body1);
       }
     )
  )

Tags:

Dart

Flutter