简体   繁体   English

如何在 flutter 中设置 Cupertino 应用程序的 textStyle

[英]How to set textStyle of a Cupertino app in flutter

I have a CupertinoApp and I would like to apply a custom TextStyle to all the screen/object of my app.我有一个 CupertinoApp,我想将自定义TextStyle应用于我的应用程序的所有屏幕/对象。 For example I would lie to set a font family to all Text widget & Dialog widget and use that font across all my app.例如,我会撒谎为所有 Text 小部件和 Dialog 小部件设置一个字体系列,并在我的所有应用程序中使用该字体。 I was hoping to set it once in CupertinoThemeData or CupertinoTextThemeData but so far I did not have joy.我希望在CupertinoThemeDataCupertinoTextThemeData中设置一次,但到目前为止我还没有感到高兴。

Note: I am able to set style for each Text however I would like to set it once for all注意:我可以为每个文本设置样式,但是我想一劳永逸地设置它

I've just run into this at the moment.我现在刚刚遇到这个。

All I'm trying to do is colour text white, with general black backgrounds across the app (not font work).我要做的就是将文本着色为白色,整个应用程序具有一般的黑色背景(不是字体工作)。

The following has brought me some success:以下为我带来了一些成功:

return CupertinoApp(
  theme: new CupertinoThemeData(
    brightness: Brightness.dark,
    primaryColor: CupertinoColors.dark,
    barBackgroundColor: CupertinoColors.black,
    scaffoldBackgroundColor: CupertinoColors.black,
    textTheme: new CupertinoTextThemeData(
      primaryColor: CupertinoColors.white,
      brightness: Brightness.light,
      textStyle: TextStyle(color: CupertinoColors.white),
      // ... here I actually utilised all possible parameters in the constructor
      // as you can see in the link underneath
    ),
  ),
  // ...
)

Ref: CupertinoTextThemeData Constructor参考: CupertinoTextThemeData 构造函数

I think you could extend my TextStyle(color: CupertinoColors.white) to apply fonts too.我想你也可以扩展我的TextStyle(color: CupertinoColors.white)来应用 fonts 。 I intend to extract the TextStyle and ...ThemeData into separate classes to create a single place to edit them.我打算将TextStyle...ThemeData提取到单独的类中,以创建一个编辑它们的地方。

Hopefully this advances your position希望这能提升您的 position

Use this example of theme in your CupertinoApp .在您的CupertinoApp中使用这个主题示例。

 theme: CupertinoThemeData(
        textTheme: CupertinoTextThemeData(
            textStyle: TextStyle(
                fontSize: 14,
                fontStyle: FontStyle.italic,
                backgroundColor: CupertinoColors.black)),
      ),

Reminder: For the colors, use a CupertinoColor instead of a simple Color.提醒:对于 colors,请使用 CupertinoColor 而不是简单的 Color。
My code is here我的代码在这里

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

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