简体   繁体   English

你能改变 flutter 文本主题吗?

[英]Can you change flutter text theme?

If the theme is set in main.dart as如果主题在 main.dart 中设置为

return MaterialApp(
  title: 'MY APP',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    fontFamily: 'Cabin',
    textTheme: TextTheme(
      headline1: TextStyle(
        fontFamily: 'Raleway',
        color: Colors.black,
        fontWeight: FontWeight.w600,
        fontSize: 18,
      ),
      subtitle1: TextStyle(
        fontFamily: 'Raleway',
        color: Colors.black54,
        fontWeight: FontWeight.w600,
        fontSize: 16,
      ),
    ),
  ),

and I'm using the theme as我正在使用主题作为

Text('MY STRING',
    style: Theme.of(context).textTheme.subtitle1),

How can I make 'MY STRING' be a different color then the subtitle1 theme color while keeping the other properties of theme data, such as font weight and family and size, etc.?如何使“我的字符串”成为与 subtitle1 主题颜色不同的颜色,同时保留主题数据的其他属性,例如字体粗细、系列和大小等?

You can use de method copyWith(color: your_color) to change properties of a TextTheme.您可以使用 de 方法copyWith(color: your_color)来更改 TextTheme 的属性。

Example:例子:

Text('MY STRING',
  style: Theme.of(context).textTheme.subtitle1
    .copyWith(color: Colors.red),
)

Doc Reference: https://api.flutter.dev/flutter/material/TextTheme/copyWith.html文档参考: https://api.flutter.dev/flutter/material/TextTheme/copyWith.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM