singleSelectFormEntry method

Widget singleSelectFormEntry({
  1. String title = 'Select Option',
  2. String subTitle = 'Choose one option from the list',
  3. required List<String> options,
  4. String? hintText,
  5. IconData iconData = Icons.list,
  6. dynamic onChanged(
    1. String?
    )?,
  7. String? defaultValue,
  8. String? validator(
    1. String?
    )?,
  9. bool isRequired = false,
})

Implementation

Widget singleSelectFormEntry({
  String title = 'Select Option',
  String subTitle = 'Choose one option from the list',
  required List<String> options,
  String? hintText,
  IconData iconData = Icons.list,
  Function(String?)? onChanged,
  String? defaultValue,
  String? Function(String?)? validator,
  bool isRequired = false,
}) {
  if (options.isEmpty) {
    throw ArgumentError('Options list cannot be empty');
  }

  if (!options.contains(defaultValue)) {
    defaultValue = null;
  }

  return formEntry(
    title: title,
    subTitle: subTitle,
    inputWidget: DropdownButtonFormField<String>(
      value: defaultValue,
      dropdownColor: Colors.white,
      style: const TextStyle(
        color: Colors.black87,
        fontSize: 16,
        fontWeight: FontWeight.w400,
      ),
      hint: Text(
        hintText ?? 'Select an option',
        style: TextStyle(
          color: Colors.grey[600],
          fontSize: 16,
          fontWeight: FontWeight.w400,
        ),
      ),
      icon: Icon(
        Icons.keyboard_arrow_down,
        color: Colors.grey[600],
        size: 24,
      ),
      decoration: elegantInputDecoration(
        hintText: '',
        prefix: Icon(iconData, color: Colors.grey[600]),
      ),
      menuMaxHeight: 300,
      items: options.map((String option) {
        return DropdownMenuItem<String>(
          value: option,
          child: Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: 8.0,
              vertical: 4.0,
            ),
            child: Text(
              option,
              style: const TextStyle(
                color: Colors.black87,
                fontSize: 16,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
        );
      }).toList(),
      onChanged: isEdit
          ? (value) {
              onChanged?.call(value);
              widget.formKey.currentState?.save();
              widget.onModified?.call();
            }
          : null,
      validator:
          validator ??
          (isRequired
              ? (value) => value == null || value.isEmpty
                    ? '$title is required'
                    : null
              : null),
    ),
  );
}