dropdownField method
Implementation
String dropdownField(String elementName, String elementType, Map<String, dynamic> map, {String? parent}) {
String items;
String initialValue;
if (map['type'] == 'enum') {
items = '''
${elementType}.values.map((value) {
maxWidth = max(value.toString().split('.').last.length * 1.0, maxWidth);
return DropdownMenuItem(
value: value.toString().split('.').last,
child: Text(value.toString().split('.').last),
);
}).toList()
''';
initialValue = '${elementType}.values.first.toString().split(\'.\').last';
} else {
items = '[' +
(map['options'] as List<Map<String, dynamic>>)
.map((e) =>
'const DropdownMenuItem<String>(value: "${e['value'].toString().split('.').last}",' +
'child: const Text("${e['label'] ?? e['value'].toString().split('.').last}"))')
.toList()
.join(',\n') +
']';
initialValue = (map['options'] as List<Map<String, dynamic>>).map((e) => "'e['value'].toString()'").toList().first;
}
return '''
SizedBox(
width: 200, // min(width, maxWidth * 10),
child:DropdownButtonFormField(
value: ${parent == null ? "_formData['$elementName'] ?? $initialValue" : "_formData['$parent']['$elementName'] ?? $initialValue"},
decoration: const InputDecoration(
labelText: '${map['label'] ?? elementName[0].toUpperCase() + elementName.substring(1)}',
hintText: '${map['hint'] ?? ''}',
helperText: '${map['helper'] ?? ''}',
),
items: $items,
onChanged: (value) => onSaved('${elementName}', value, parent: '${parent ?? ''}'),
), // DropdownButtonFormField
) // SizedBox
''';
}