buildDropdown method Null safety

  1. @protected
Widget buildDropdown(
  1. {required BuildContext context,
  2. required String id,
  3. String? defaultValue,
  4. required List<String> items,
  5. required TranslationEntry label,
  6. List<ValueValidator>? validators,
  7. required Map<String, dynamic> values}
)

Implementation

@protected
Widget buildDropdown({
  required BuildContext context,
  required String id,
  String? defaultValue,
  required List<String> items,
  required TranslationEntry label,
  List<ValueValidator>? validators,
  required Map<String, dynamic> values,
}) {
  if (values[id] == null && defaultValue != null) {
    values[id] = defaultValue;
  }

  var translator = Translator.of(context);
  return DropdownButtonFormField<String>(
    autovalidateMode: AutovalidateMode.always,
    decoration: InputDecoration(
      labelText: translator.translate(label),
    ),
    items: [
      for (var item in items)
        DropdownMenuItem(
          value: item,
          child: Text(item),
        ),
    ],
    onChanged: (value) => values[id] = value,
    value: values[id]?.toString(),
    validator: (value) => validators?.isNotEmpty == true
        ? Validator(validators: validators!).validate(
            context: context,
            label: translator.translate(label),
            value: value,
          )
        : null,
  );
}