如何在扑扑的特定容器中设置所有文本的颜色?

Ste*_*mes 0 dart flutter

我不想更改整个应用程序的。只是一个容器。我可以用其他小部件或其他包装吗?

Tok*_*yet 9

我认为颤振的答案很好。但它的力量ThemeData远超你的想象。这是有关应用程序部分主题官方文档

您可以提供 aTheme来包装您的容器以提供新主题。这里有两种方法来slove它:

1. 创建独特的 ThemeData

/*Not recommended, this could make a totally different If you just want a little part changed.*/
Theme(
  // Create a unique theme with "ThemeData"
  data: ThemeData(
    textTheme: /* Your Text Theme*/,
  ),
  child: Container(
    onPressed: () {},
    child: Text("Your Text Here"),
  ),
);
Run Code Online (Sandbox Code Playgroud)

2.扩展父主题

Theme(
  // Find and extend the parent theme using "copyWith". See the next
  // section for more info on `Theme.of`.
  data: Theme.of(context).copyWith(textTheme: /* Provide your theme here! */),
  child: Container(
    child: Text("your text here"),
  ),
);
Run Code Online (Sandbox Code Playgroud)

您也可以使用现有主题稍作更改:

Theme.of(context).textTheme.copyWith(
  body1: Theme.of(context).textTheme.body1.copyWith(color: Colors.red),
)
Run Code Online (Sandbox Code Playgroud)


DaG*_*ner 5

TextStyle仅将某些属性应用于应用程序的子树。您可以使用DefaultTextStyle

DefaultTextStyle(
  child: Container(child: /* your subtree */),
  style: TextStyle(color: Colors.red),
),
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这将替换所有默认值 - 不仅仅是颜色 (2认同)