fromJson static method
TDThemeData?
fromJson(
- String name,
- String themeJson, {
- dynamic recoverDefault = false,
- TDExtraThemeData? extraThemeData,
解析配置的json文件为主题数据
Implementation
static TDThemeData? fromJson(String name, String themeJson,
{var recoverDefault = false, TDExtraThemeData? extraThemeData}) {
if (themeJson.isEmpty) {
Log.e('TTheme', 'parse themeJson is empty');
return null;
}
try {
/// 要求json配置必须正确
final themeConfig = json.decode(themeJson);
if (themeConfig.containsKey(name)) {
var theme = _emptyData(name);
Map<String, dynamic> curThemeMap = themeConfig['$name'];
/// 设置颜色
Map<String, dynamic>? colorsMap = curThemeMap['color'];
colorsMap?.forEach((key, value) {
theme.colorMap[key] = toColor(value);
});
/// 设置字体尺寸
Map<String, dynamic>? fontsMap = curThemeMap['font'];
fontsMap?.forEach((key, value) {
theme.fontMap[key] =
Font.fromJson(value);
});
/// 设置圆角
Map<String, dynamic>? cornersMap = curThemeMap['radius'];
cornersMap?.forEach((key, value) {
theme.radiusMap[key] = value.toDouble();
});
/// 设置字体
Map<String, dynamic>? fontFamilyMap = curThemeMap['fontFamily'];
fontFamilyMap?.forEach((key, value) {
theme.fontFamilyMap[key] = FontFamily(fontFamily: value['fontFamily']);
});
/// 设置阴影
Map<String, dynamic>? shadowMap = curThemeMap['shadow'];
shadowMap?.forEach((key, value) {
var list = <BoxShadow>[];
(value as List).forEach((element) {
list.add(BoxShadow(
color: toColor(element['color']),
blurRadius: element['blurRadius'].toDouble(),
spreadRadius: element['spreadRadius'].toDouble(),
offset: Offset(element['offset']?['x'].toDouble() ?? 0,
element['offset']?['y'].toDouble() ?? 0),
));
});
theme.shadowMap[key] = list;
});
/// 设置Margin
Map<String, dynamic>? marginsMap = curThemeMap['margin'];
marginsMap?.forEach((key, value) {
theme.spacerMap[key] = value.toDouble();
});
if (extraThemeData != null) {
extraThemeData.parse(name, curThemeMap);
theme.extraThemeData = extraThemeData;
}
if (recoverDefault) {
_defaultThemeData = theme;
}
return theme;
} else {
Log.e('TTheme',
'load theme error ,not found the theme with name:${name}');
return null;
}
} catch (e) {
Log.e('TTheme', 'parse theme data error:${e}');
return null;
}
}