getTextField function

Widget getTextField(
  1. TextEditingController controller, {
  2. void onChanged(
    1. String
    )?,
  3. FocusNode? focusNode,
  4. String? label,
  5. String? hint,
  6. String? icon,
  7. void iconOnTap()?,
  8. Color? iconColor,
  9. String? id,
  10. bool? showPass,
  11. List<MahasMessageModel>? validation,
  12. String? rightIcon,
  13. Color? rightIconColor,
  14. Function? rightIconFieldFunction,
  15. bool readOnly = false,
  16. bool allowActionWhenReadOnly = false,
  17. Color? colorSet,
  18. void onTap()?,
  19. List<TextInputFormatter>? inputFormatters,
  20. TextInputType? keyboardType,
  21. TextAlign? textAlign,
  22. Function? onBlur,
})

Implementation

Widget getTextField(
  TextEditingController controller, {
  void Function(String)? onChanged,
  FocusNode? focusNode,
  String? label,
  String? hint,
  String? icon,
  void Function()? iconOnTap,
  Color? iconColor,
  String? id,
  bool? showPass,
  List<MahasMessageModel>? validation,
  String? rightIcon,
  Color? rightIconColor,
  Function? rightIconFieldFunction,
  bool readOnly = false,
  bool allowActionWhenReadOnly = false,
  Color? colorSet,
  void Function()? onTap,
  List<TextInputFormatter>? inputFormatters,
  TextInputType? keyboardType,
  TextAlign? textAlign,
  Function? onBlur,
}) {
  bool isAutoFocus = false;
  bool isValid = true;
  if (id != null && validation != null) {
    isValid = validation
            .firstWhereOrNull((element) => element.inputName == id)
            ?.errorMessages
            .isEmpty ??
        true;
  }
  return StatefulBuilder(
    builder: (context, setState) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (label != null) ...[
            getSpaceHeight(.5),
            getCustomText(label, color: MahasColors.greyColor),
          ],
          Container(
            width: double.infinity,
            height: MahasDimensions.getInputHeight(),
            margin: EdgeInsets.symmetric(
              vertical: MahasDimensions.getHeightPercentSize(
                  MahasDimensions.inputMargin),
            ),
            padding: EdgeInsets.symmetric(
              horizontal: MahasDimensions.getHeightPercentSize(
                  MahasDimensions.inputPadding),
            ),
            decoration: MahasDimensions.getTextFieldDecoration(
              colorSet: colorSet ??
                  (isAutoFocus
                      ? MahasColors.primaryColor
                      : !isValid
                          ? MahasColors.dangerColor.withOpacity(.4)
                          : MahasColors.inputBorderColor),
            ),
            child: InkWell(
              onTap: () {
                if (onTap != null && !readOnly) {
                  onTap();
                }
                focusNode?.requestFocus();
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  if (icon != null)
                    InkWell(
                      onTap: allowActionWhenReadOnly
                          ? iconOnTap
                          : readOnly
                              ? null
                              : iconOnTap,
                      child: getIcon(icon,
                          color: iconColor ?? MahasColors.greyColor),
                    ),
                  if (icon != null) getSpaceWidth(MahasDimensions.inputPadding),
                  Expanded(
                    flex: 1,
                    child: Focus(
                      onFocusChange: (hasFocus) {
                        if (hasFocus) {
                          setState(() => isAutoFocus = true);
                        } else {
                          setState(() => isAutoFocus = false);
                          if (onBlur != null) onBlur();
                        }
                      },
                      child: TextField(
                        onChanged: onChanged,
                        obscureText: showPass == null ? false : !showPass,
                        controller: controller,
                        autofocus: false,
                        focusNode: focusNode,
                        readOnly: readOnly,
                        onTap: onTap,
                        inputFormatters: inputFormatters,
                        keyboardType: keyboardType,
                        textAlign: textAlign ?? TextAlign.start,
                        style: TextStyle(
                          fontSize: MahasDimensions.getInputTextSize(),
                          color: MahasColors.blackColor,
                          fontWeight: FontWeight.w400,
                        ),
                        decoration: InputDecoration(
                          isDense: true,
                          contentPadding: EdgeInsets.zero,
                          hintText: label ?? hint,
                          border: InputBorder.none,
                        ),
                      ),
                    ),
                  ),
                  if (rightIconFieldFunction != null && rightIcon != null)
                    InkWell(
                      onTap: allowActionWhenReadOnly
                          ? () => rightIconFieldFunction()
                          : readOnly
                              ? null
                              : () => rightIconFieldFunction(),
                      child: SizedBox(
                        height: MahasDimensions.getInputHeight(),
                        child: Center(
                          child: getIcon(rightIcon,
                              color: rightIconColor ?? MahasColors.greyColor),
                        ),
                      ),
                    )
                ],
              ),
            ),
          )
        ],
      );
    },
  );
}