我正在尝试设置我的字体MaterialApp
。由于我使用的是深色主题,所以我想使用copyWith
然后更改fontFamily
。然而,copyWith
没有更改的选项fontFamily
.
MaterialApp(
theme: ThemeData.dark().copyWith(
fontFamily: 'MyFontFamily',
),
未定义命名参数“fontFamily”。
如何保留深色主题并更改字体系列? (同样的问题ThemeData.light()
.)
我找到了解决方案并在下面发布。
如果你看一下源代码 https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/theme_data.dart for ThemeData.light()
and ThemeData.dark()
,你可以看到它所做的只是设置brightness
value:
/// A default light blue theme.
///
/// This theme does not contain text geometry. Instead, it is expected that
/// this theme is localized using text geometry using [ThemeData.localize].
factory ThemeData.light() => ThemeData(brightness: Brightness.light);
/// A default dark theme with a teal secondary [ColorScheme] color.
///
/// This theme does not contain text geometry. Instead, it is expected that
/// this theme is localized using text geometry using [ThemeData.localize].
factory ThemeData.dark() => ThemeData(brightness: Brightness.dark);
这意味着无需费心就能解决您的问题ThemeData.light()
or ThemeData.dark()
。只需创建一个新的ThemeData
并设置brightness
除了你自己fontFamily
:
MaterialApp(
theme: ThemeData(
brightness: Brightness.dark,
fontFamily: 'MyFontFamily',
),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)