我试着为文字风格制作一个应用程序主题.我从这个例子中得到了flutter docs个.

MaterialApp(
  title: appName,
  theme: ThemeData(
    // Define the default brightness 和 colors.
    brightness: Brightness.dark,
    primaryColor: Colors.lightBlue[800],

    // Define the default font family.
    fontFamily: 'Georgia',

    // Define the default `TextTheme`. Use this to specify the default
    // text styling for headlines, titles, bodies of text, 和 more.
    textTheme: const TextTheme(
      displayLarge: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
      titleLarge: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
      bodyMedium: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
    ),
  ),
  home: const MyHomePage(
    title: appName,
  ),
);

Container(
  color: Theme.of(context).colorScheme.secondary,
  child: Text(
    'Text with a background color',
    style: Theme.of(context).textTheme.titleLarge,
  ),
),

我想要有选项来添加相同文本大小的多种 colored颜色 .例如,titleLarge可以是红色或白色.

在TextTheme->TextStyle中添加 colored颜色 只能提供添加一种 colored颜色 的选项.

有什么方法可以让我稍后在文本小部件中更改它吗?也许有一种方法可以覆盖Theme.of(context).textTheme.titleLarge的 colored颜色 ?

推荐答案

您可以复制主题,然后像这样给出单独的文本样式

Theme.of(context).textTheme.apply(bodyColor: Colors.white).titleLarge

或者,您也可以像这样使用条件?来使用copyWith

Theme.of(context).textTheme.titleLarge?.copyWith(color: Colors.white)

或者像这样的零支票!

Theme.of(context).textTheme.titleLarge!.copyWith(color: Colors.white)

Flutter相关问答推荐

具有可变高度小部件的SingleChildScrollView内的列

为什么他们实例化类,然后丢弃该实例?

Flutter 蜂窝单元测试

本机调试符号包含无效的目录调试符号.目前仅支持Android ABI

如何将Will POP示波器转换为POP数据抖动的POP示波器

如何在抖动中将PNG图像转换为WebP

任务';:app:check DebugDuplicateClasss';的Ffltter Share_plus抛出执行失败

顶部对齐ListTile的[前导、标题、尾随]小部件

Riverpod Provider.family 和 HookConsumerWidget 不刷新 UI

如何将 Flutter 项目迁移到具有多个插件的 Gradle 8?

启动画面中的淡入淡出图像

如何让小部件根据小部件的大小构建不同的布局?

flutter 中 // 和 /// 有什么区别

Flutter 中出现错误空判断运算符用于空值

在 Flutter 中更改高架按钮 OnPressed 的背景 colored颜色

Flutter如何从左到右连续 Select 多个列表项而不跳过任何

Firebase Cloud Messaging (Flutter):订阅主题时出现错误消息

解密 Modulr 安全令牌

阴影效果只出现在一张卡片的左边 在Flutter 中?

在 Flutter Bloc 中具有称为 status 的属性的一种状态或不同的状态,哪个更可取?