createTheme static method
AFGeneratedFile
createTheme(
- AFCommandContext context,
- String uiName,
- AFCommandArgumentsParsed args, {
- String? fullId,
- AFLibraryID? fromLib,
Implementation
static AFGeneratedFile createTheme(AFCommandContext context, String uiName, AFCommandArgumentsParsed args, {
String? fullId,
AFLibraryID? fromLib,
}) {
final generator = context.generator;
final parentTheme = args.accessNamed(argParentTheme);
final parentThemeID = args.accessNamed(argParentThemeID);
final isCustomParent = parentTheme != generator.nameDefaultParentTheme;
final isOverride = fullId != null;
final pathTheme = generator.pathTheme(uiName, isCustomParent: isCustomParent);
if(pathTheme == null) {
throw AFException("Could not generate theme path");
}
if(isCustomParent) {
if(parentThemeID == generator.nameDefaultParentThemeID) {
throw AFCommandError(error: "You specified $parentTheme as the parent theme, you must also specify its full theme id using --$argParentThemeID");
}
}
// create the theme file itself.
final fileTheme = context.createFile(pathTheme, ThemeT.core(), insertions: {
AFSourceTemplate.insertMainTypeInsertion: uiName,
AFSourceTemplate.insertMainParentTypeInsertion: parentTheme
});
final imports = <String>[];
if(isCustomParent && fromLib != null) {
var parentThemePackage = fromLib.name;
final import = SnippetImportFromPackageT().toBuffer(context, insertions: {
AFSourceTemplate.insertPackageNameInsertion: parentThemePackage,
AFSourceTemplate.insertPackagePathInsertion: "${fromLib.codeId}_flutter.dart"
});
imports.addAll(import.lines);
}
fileTheme.importAll(context, imports);
// add the line that installs it
final fileDefineUI = generator.modifyFile(context, generator.pathDefineCore);
final defineTheme = context.createSnippet(SnippetCallDefineThemeT(), insertions: {
SnippetCallDefineThemeT.insertThemeID: parentThemeID,
SnippetCallDefineThemeT.insertThemeType: uiName,
});
fileDefineUI.addLinesAfter(context, AFCodeRegExp.startDefineThemes, defineTheme.lines);
if(imports.isNotEmpty) {
fileDefineUI.addLinesBefore(context, AFCodeRegExp.startDefineCore, imports);
if(fromLib != null) {
fileDefineUI.importIDFile(context, fromLib);
}
}
fileDefineUI.importFile(context, fileTheme);
if(!isOverride) {
generator.addExportsForFiles(context, args, [fileTheme]);
}
return fileTheme;
}