dropdownHideUnderlineField method

String dropdownHideUnderlineField(
  1. String elementName,
  2. String elementType,
  3. Map<String, dynamic> map, {
  4. String? parent,
})

Implementation

String dropdownHideUnderlineField(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: min(width, maxWidth * 10),
    child: DropdownButtonHideUnderlineFormField(
      child: ButtonTheme(
        alignedDropdown: true,
        child: DropdownButton(
          value: ${parent == null ? "_formData['$elementName'] ?? $initialValue" : "_formData['$parent']['$elementName'] ?? $initialValue"},
          items: $items,
          onChanged:  (value) => onSaved('${elementName}', value, parent: '${parent ?? ''}'),
          ${buildValidator(map['validators'] as List?)}
        ), // DropdownButton
      ), // ButtonTheme
    ), // DropdownButtonHideUnderlineFormField
  ) // SizedBox
  ''';
}