【发布时间】:2021-08-14 07:50:51
【问题描述】:
根据this answer,我所要做的就是用Theme 包装一个小部件并提供ThemeData。我从build 方法返回一个小部件,如下所示:
Theme( // wrapping `Card` widget with a theme
data: Theme.of(context).copyWith( // extend main theme
textTheme: Theme.of(context).textTheme.copyWith( // extend main text theme
subtitle1: TextStyle(
color: Colors.white, // subtitle text color to white
),
caption: TextStyle(
color: Colors.white, // caption text color to white
),
),
),
child: Card( // card widget as a child of theme
// ... other stuff ...
child: Container(
margin: _margin,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
profile.alias,
style: Theme.of(context).textTheme.subtitle1, // set text style to subtitle1
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Joined ${timeago.format(profile.dateJoined)}',
style: Theme.of(context).textTheme.caption, // set text style to subtitle2
textAlign: TextAlign.right,
),
Text(
'Has ${profile.stats.subscriberCount == null ? 0 : profile.stats.subscriberCount} subscribers',
style: Theme.of(context).textTheme.caption, // set text style to caption
textAlign: TextAlign.right,
),
Text(
'Following ${profile.stats.followingCount == null ? 0 : profile.stats.followingCount} people',
style: Theme.of(context).textTheme.caption, // set text style to caption
textAlign: TextAlign.right,
),
],
),
),
],
)),
),
);
奇怪的是,这不会影响我的文字。它仍然保持黑色。
可能有一些我不理解的主题。有没有办法将主题(在本例中为文本颜色)部分应用到影响所有直接和间接子级的小部件?
环境
- 颤振 2.0.6
- 飞镖 2.12.3
【问题讨论】:
-
你在项目中定义主题的地方也定义了文本的颜色,所以当你调用主题数据时,它会反映在你的卡片上试试这个
-
使用
Theme()你已经改变了Theme的child不要再次使用style: Theme.of(context).textTheme.subtitle1和Text()小部件使用bodyText2主题所以而不是使用textTheme.subtitle1更改它到textTheme.bodyText2试试这样。添加这样的属性style: Theme.of(context).textTheme.subtitle1你再次设置MaterialApp()的属性而不是Theme()。
标签: flutter