user6274128
user6274128

Reputation:

Flutter ThemeData is not working for the Text

Code:

@override
Widget build(BuildContext context) {
  return Theme(
    data: ThemeData(textTheme: TextTheme(body1: TextStyle(fontSize: 40))),
    child: Text("Hello World!"), // size not changing
  );
}

But when I use

data: ThemeData(textTheme: TextTheme(body1: TextStyle(fontSize: 40))),

in my MaterialApp's theme then size of the Text gets changed.


PS: I know I can give Text a size by using style: property but I wanna know why my code isn't changing Text font size.

Upvotes: 5

Views: 2476

Answers (1)

Rémi Rousselet
Rémi Rousselet

Reputation: 277057

Text does not use Theme. Theme is material design specific, while Text is general purpose.

What Text uses is DefaultTextStyle, which is edited by MaterialApp (or some other widgets such as AppBar) with values from the Theme.

The following should work:

DefaultTextStyle(
  style: TextStyle(fontSize: 40),
  child: Text("Hello World"),
);

Upvotes: 6

Related Questions