Flutter:使用 .copyWith 更改 ThemeData 中的字体系列

Flutter: Change font family in ThemeData with .copyWith

我知道您可以像这样用自己的字体定义一个新的 ThemeData 对象:

ThemeData(fontFamily: 'Roboto')

但是,我想使用 copyWith 方法复制现有的 ThemeData:

ThemeData.dark().copyWith(...)

遗憾的是,copyWith 中没有参数 fontFamiliy。我只想更改 fontFamily 而不是定义一个全新的 TextTheme.

如何复制现有 ThemeData 并更改其字体系列?

尝试这样的事情:

ThemeData darkTheme = ThemeData.dark().copyWith(
    textTheme: ThemeData.dark().textTheme.apply(
          fontFamily: 'Roboto',
        ),
    primaryTextTheme: ThemeData.dark().textTheme.apply(
          fontFamily: 'Roboto',
        ),
    accentTextTheme: ThemeData.dark().textTheme.apply(
          fontFamily: 'Roboto',
        ),
); 

如果你查看源代码,它是一回事:

这几乎是同一件事:https://github.com/flutter/flutter/issues/41276

我不知道为什么你需要复制字体,但有时在主题 MaterialApp 中设置 fontFamily 是有意义的。关于这个有:Changing the font family in Flutter when using ThemeData.dark() or ThemeData.light()