Flutter ThemeData is not working for the Text

2019-06-24 07:39发布

问题:

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.

回答1:

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"),
);


标签: dart flutter