getTextField function
Widget
getTextField(
- TextEditingController controller, {
- void onChanged()?,
- FocusNode? focusNode,
- String? label,
- String? hint,
- String? icon,
- void 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 onTap()?,
- List<
TextInputFormatter> ? inputFormatters, - TextInputType? keyboardType,
- TextAlign? textAlign,
- 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),
),
),
)
],
),
),
)
],
);
},
);
}