mahasInputSelectWidget<T> function

Widget mahasInputSelectWidget<T>({
  1. MahasInputModel? controller,
  2. bool showLabel = true,
  3. String? icon,
  4. String? label,
  5. required dynamic getText(
    1. T
    ),
  6. void additionalOnChanged(
    1. T?
    )?,
  7. bool readOnly = false,
  8. List<MahasMessageModel>? validation,
  9. int? index,
})

Implementation

Widget mahasInputSelectWidget<T>({
  MahasInputModel? controller,
  bool showLabel = true,
  String? icon,
  String? label,
  required Function(T) getText,
  void Function(T?)? additionalOnChanged,
  bool readOnly = false,
  List<MahasMessageModel>? validation,
  int? index,
}) {
  final MahasInputSelectModel<T>? con =
      controller is MahasInputSelectModel<T> ? controller : null;
  if (con == null) return const SizedBox.shrink();
  label = showLabel ? label ?? con.label : null;
  final String key = '${con.id}${index != null ? '_$index' : ''}';
  final bool isValid = validation
          ?.firstWhereOrNull((element) => element.inputName == key)
          ?.errorMessages
          .isEmpty ??
      true;

  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      if (label != null) ...[
        getSpaceHeight(.5),
        getCustomText(label, color: MahasColors.greyColor),
      ],
      Padding(
        padding: EdgeInsets.symmetric(
          vertical: MahasDimensions.getHeightPercentSize(1.2),
        ),
        child: SizedBox(
          height: MahasDimensions.getInputHeight(),
          child: Container(
            padding: EdgeInsets.symmetric(
              horizontal: MahasDimensions.getWidthPercentSize(2.5),
            ),
            decoration: MahasDimensions.getTextFieldDecoration(
              colorSet: isValid
                  ? MahasColors.inputBorderColor
                  : MahasColors.dangerColor.withOpacity(.4),
            ),
            child: Center(
              child: Obx(
                () => DropdownButton<T>(
                  dropdownColor: MahasColors.backgroundColor,
                  isExpanded: true,
                  itemHeight: null,
                  isDense: true,
                  underline: getSpaceHeight(0),
                  focusNode: con.focusNode,
                  items: con.list.map((T model) {
                    return DropdownMenuItem<T>(
                      value: model,
                      child: Row(
                        children: [
                          if (icon != null) ...[
                            getIcon(icon, color: MahasColors.greyColor),
                            getSpaceWidth(1),
                          ],
                          Expanded(
                            child: Text(
                              getText(model),
                              style: MahasDimensions.getDropdownItemTextStyle(),
                              overflow: TextOverflow.ellipsis,
                            ),
                          )
                        ],
                      ),
                    );
                  }).toList(),
                  onChanged: readOnly
                      ? null
                      : (T? value) {
                          if (value != null) {
                            con.selected.value = value;
                            additionalOnChanged?.call(value);
                          }
                        },
                  value: con.selected.value,
                ),
              ),
            ),
          ),
        ),
      ),
    ],
  );
}