generateForAnnotatedElement method
dynamic
generateForAnnotatedElement(
- Element element,
- ConstantReader annotation,
- BuildStep buildStep
Implement to return source code to generate for element
.
This method is invoked based on finding elements annotated with an
instance of T
. The annotation
is provided as a ConstantReader
.
Supported return values include a single String or multiple String instances within an Iterable or Stream. It is also valid to return a Future of String, Iterable, or Stream.
Implementations should return null
when no content is generated. Empty
or whitespace-only String instances are also ignored.
Implementation
@override
generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
Map<String, Section> sections = {};
StringBuffer codeBuffer = new StringBuffer("\n");
var head = annotation.read("head").literalValue;
if (head != null) {
String headStr = head as String;
codeBuffer.writeln(headStr);
}
codeBuffer.write(classHead(element.name!));
if (element.kind == ElementKind.CLASS) {
for (var e in ((element as ClassElement).fields)) {
print("===start field:" + e.name);
var sectionAnnotation = _textSettingChecker.firstAnnotationOf(e);
if (sectionAnnotation != null) {
var reader = ConstantReader(sectionAnnotation);
var sectionName = reader.read('sectionName').literalValue as String;
final title = reader.read('title').literalValue as String;
final hintCheck = reader.read('hint').literalValue;
final icon = reader.read('icon').literalValue;
final obscure = reader.read('obscure').literalValue;
final condition = reader.read('condition').literalValue;
var hint = "";
if (hintCheck != null) {
hint = hintCheck as String;
}
var section = sections[sectionName];
if (section == null) {
section = new Section(sectionName);
sections[sectionName] = section;
}
if (e.type.isDartCoreString) {
var tft = TextFieldType.text;
if (obscure != null) {
bool isObsucre = obscure as bool;
if (isObsucre) tft = TextFieldType.password;
}
if (icon != null)
section.fields.add(new TextField(e.name, condition, title, hint,
icon: (icon as String), fieldType: tft));
else {
section.fields.add(new TextField(e.name, condition, title, hint,
fieldType: tft));
}
} else if (e.type.isDartCoreDouble) {
if (icon != null)
section.fields.add(new TextField(e.name, condition, title, hint,
icon: (icon as String),
fieldType: TextFieldType.number_double));
else {
section.fields.add(new TextField(e.name, condition, title, hint,
fieldType: TextFieldType.number_double));
}
} else if (e.type.isDartCoreInt) {
if (icon != null)
section.fields.add(new TextField(e.name, condition, title, hint,
icon: (icon as String), fieldType: TextFieldType.number_int));
else {
section.fields.add(new TextField(e.name, condition, title, hint,
fieldType: TextFieldType.number_int));
}
}
} else {
var optionAnnotation = _optionSettingChecker.firstAnnotationOf(e);
if (optionAnnotation != null) {
var reader = ConstantReader(optionAnnotation);
var sectionName = reader.read('sectionName').literalValue as String;
final title = reader.read('title').literalValue as String;
var options = reader.read('options').literalValue as String;
final condition = reader.read('condition').literalValue;
var section = sections[sectionName];
if (section == null) {
section = new Section(sectionName);
sections[sectionName] = section;
}
section.fields.add(new OptionField(
e.name,
condition,
e.type.getDisplayString(withNullability: true),
title,
options));
} else {
var boolAnnotation = _boolSettingChecker.firstAnnotationOf(e);
if (boolAnnotation != null) {
final reader = ConstantReader(boolAnnotation);
final sectionName =
reader.read('sectionName').literalValue as String;
final title = reader.read('title').literalValue as String;
final condition = reader.read('condition').literalValue;
var section = sections[sectionName];
if (section == null) {
section = new Section(sectionName);
sections[sectionName] = section;
}
section.fields.add(new BoolFeild(e.name, condition, title));
}
}
}
}
}
var sectionId = 1;
sections.forEach((k, section) {
codeBuffer..writeln(addSection(section, sectionId));
sectionId++;
});
codeBuffer.writeln("]);");
codeBuffer.writeln("}");
sectionId = 1;
sections.forEach((k, section) {
codeBuffer..write(startBuildSection(section, sectionId));
section.fields.forEach((field) {
if (field is TextField) {
codeBuffer.write(startAddTile(field.condition));
codeBuffer.write(textInputTile(field.fieldType, field.title,
field.name, field.hint, field.icon));
codeBuffer.write(endAddTile());
} else if (field is OptionField) {
codeBuffer.write(startAddTile(field.condition));
codeBuffer.write(
optionTile(field.title, field.type, field.name, field.options));
codeBuffer.write(endAddTile());
} else if (field is BoolFeild) {
codeBuffer.write(startAddTile(field.condition));
codeBuffer.write(swithTile(field.title, field.name));
codeBuffer.write(endAddTile());
}
});
codeBuffer..writeln(endBuildSection());
sectionId++;
});
codeBuffer.writeln("}");
return codeBuffer.toString();
}