【发布时间】:2021-06-10 20:04:38
【问题描述】:
如何在 ThemeData 中将 fontFamily 与 copyWith 一起使用或如何修复我的以下错误?
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer2<ThemeNotifier, SettingNotifier>(builder:
(context, ThemeNotifier theme, SettingNotifier settings, child) {
return MaterialApp(
theme: theme.getTheme().copyWith(
textTheme: theme.getTheme()
.textTheme
.apply(fontFamily: settings.getFontSetting()),
),
debugShowCheckedModeBanner: false,
home: MainHomePage(),
);
});
}
}
主题数据类如何在主题数据中添加 fontFamily 或在 copyWith 中添加文本主题我想在 MaterialApp 中将 textTheme 和 copyWith 添加到 getFonts 任何人都可以解决这个问题......................................................
class ThemeNotifier with ChangeNotifier {
ThemeData _themeData;
ThemeData getTheme() => _themeData;
ThemeNotifier() {
ThemeManager.readData('themeMode').then((value) {
print('App Theme value read from storage: ' + value.toString());
var themeMode = value;
if (themeMode == 'yellow') {
_themeData = yellow;
} else {
if (themeMode == 'red') {
_themeData = red;
} else {
if (themeMode == 'green') {
_themeData = green;
} else {
_themeData = orange;
}
}
}
notifyListeners();
});
}
void setOrangeTheme() async {
_themeData = orange;
ThemeManager.saveData('themeMode', 'orange');
notifyListeners();
}
void setRedTheme() async {
_themeData = red;
ThemeManager.saveData('themeMode', 'red');
notifyListeners();
}
void setYellowTheme() async {
_themeData = yellow;
ThemeManager.saveData('themeMode', 'yellow');
notifyListeners();
}
void setGreenTheme() async {
_themeData = green;
ThemeManager.saveData('themeMode', 'green');
notifyListeners();
}
}
【问题讨论】:
-
嘿,欢迎来到 StackOverflow!添加问题时,您应该添加当前拥有的尽可能多的信息,因此如果您添加一些关于您得到什么错误、从
theme.getTheme()返回什么等等的上下文将会很有帮助。 -
谢谢,theme.getTheme() = ThemeData 我用于更改具有共享偏好的应用主题
-
你得到了什么错误?
-
方法“copyWith”被称为空值。
-
所以这意味着你的 ThemeNotifier 此时为空。您应该提供有关通知器及其提供位置的更多详细信息。
标签: flutter