dropdownFieldBuilder function

Widget dropdownFieldBuilder(
  1. BuildContext context,
  2. ChampionFormController controller,
  3. List<MultiselectOption> choices,
  4. ChampionOptionSelect field,
  5. FieldState currentState,
  6. FieldColorScheme currentColors,
  7. List<String>? defaultValue,
  8. dynamic updateFocus(
    1. bool focused
    ),
  9. dynamic updateSelectedOption(
    1. MultiselectOption? selectedOption
    ),
)

Implementation

Widget dropdownFieldBuilder(
  BuildContext context,
  ChampionFormController controller,
  List<MultiselectOption> choices,
  ChampionOptionSelect field,
  FieldState currentState,
  FieldColorScheme currentColors,
  List<String>? defaultValue,
  Function(bool focused) updateFocus,
  Function(MultiselectOption? selectedOption) updateSelectedOption,
) {
  return MultiselectWidget(
    id: field.id,
    controller: controller,
    requestFocus: field.requestFocus,
    child: DropdownButtonFormField<String>(
      value: defaultValue != null && defaultValue.isNotEmpty
          ? defaultValue.first
          : null,
      dropdownColor: currentColors.backgroundColor,
      items: field.options
          .map((option) => DropdownMenuItem<String>(
                value: option.value.toString(),
                child: Text(
                  option.label,
                  style: TextStyle(
                    color: currentColors.textColor,
                  ),
                ),
              ))
          .toList(),
      onSaved: field.onSubmit != null
          ? (String? value) {
              final FormResults results = FormResults.getResults(
                  controller: controller, fields: [field]);
              field.onSubmit!(results);
            }
          : null,
      onChanged: (String? value) {
        // Find the value we're going to pass.
        if (value != null) {
          final selectedOption =
              field.options.firstWhere((val) => value == val.value.toString());

          updateSelectedOption(selectedOption);
        } else {
          updateSelectedOption(null);
        }

        // Handle onchanged behavior
        if (field.onChange != null) {
          final FormResults results =
              FormResults.getResults(controller: controller, fields: [field]);

          field.onChange!(results);
        }
      },
      decoration: getInputDecorationFromScheme(currentColors)?.copyWith(
        prefixIcon: field.leading,
        suffixIcon: field.trailing,
        icon: field.icon,
      ),
    ),
  );
}