本文介绍了Flutter ThemeData不适用于文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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

但是当我使用

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

在我的 MaterialApp 主题中,然后文本已更改。

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

PS :我知道我可以给 Text 通过使用 style:属性设置大小,但是我想知道为什么我的代码没有更改 Text 字体尺寸。

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.

推荐答案

不使用 。 是特定于材料设计的,而是通用的。

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

什么使用的是,由 MaterialApp (或其他一些小部件,例如 AppBar ),其值来自。

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

以下应该起作用:

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

这篇关于Flutter ThemeData不适用于文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 01:09
查看更多