441 lines
15 KiB
Dart
441 lines
15 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:citycards_customer/common_packages/custom_text.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class CustomTextField extends StatelessWidget {
|
|
final String label;
|
|
final String hint;
|
|
final TextEditingController controller;
|
|
final int? maxLines;
|
|
final bool enabled;
|
|
final String? Function(String?)? validator;
|
|
final TextInputType? keyboardType;
|
|
final bool obscureText;
|
|
final Widget? suffixIcon;
|
|
final Widget? prefixWidget;
|
|
final void Function(String)? onChanged;
|
|
|
|
final int? maxLength;
|
|
final bool numbersOnly;
|
|
|
|
final bool isMobileNumber;
|
|
final bool isEmail;
|
|
final bool onlyLetters;
|
|
final bool noSpace;
|
|
|
|
final bool noSpecialCharacters;
|
|
final bool isFirstLetterCapital;
|
|
final int mobileLength;
|
|
final bool isPreview;
|
|
|
|
const CustomTextField({
|
|
super.key,
|
|
required this.label,
|
|
required this.hint,
|
|
required this.controller,
|
|
this.maxLines = 1,
|
|
this.enabled = true,
|
|
this.validator,
|
|
this.keyboardType,
|
|
this.obscureText = false,
|
|
this.suffixIcon,
|
|
this.prefixWidget,
|
|
this.onChanged,
|
|
this.maxLength,
|
|
this.numbersOnly = false,
|
|
this.isMobileNumber = false,
|
|
this.isEmail = false,
|
|
this.onlyLetters = false,
|
|
this.noSpace = false,
|
|
this.noSpecialCharacters = false,
|
|
this.isFirstLetterCapital = false,
|
|
this.mobileLength = 10,
|
|
this.isPreview = false,
|
|
});
|
|
|
|
void _capitalizeFirstLetter(String value) {
|
|
if (value.isEmpty) return;
|
|
final capitalized = value[0].toUpperCase() + value.substring(1);
|
|
if (capitalized != value) {
|
|
controller.value = controller.value.copyWith(
|
|
text: capitalized,
|
|
selection: TextSelection.collapsed(offset: capitalized.length),
|
|
);
|
|
}
|
|
}
|
|
|
|
String? _internalValidator(String? value) {
|
|
if (isPreview) return null;
|
|
if (value == null || value.trim().isEmpty) {
|
|
return 'Please enter $label';
|
|
}
|
|
if (isEmail) {
|
|
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
|
if (!emailRegex.hasMatch(value.trim())) {
|
|
return 'Please enter a valid email address';
|
|
}
|
|
}
|
|
if (isMobileNumber) {
|
|
if (!RegExp(r'^\d+$').hasMatch(value)) {
|
|
return 'Only numbers are allowed';
|
|
}
|
|
if (value.length != mobileLength) {
|
|
return 'Mobile number must be $mobileLength digits';
|
|
}
|
|
}
|
|
if (noSpace && value.contains(' ')) {
|
|
return 'Spaces are not allowed';
|
|
}
|
|
if (noSpecialCharacters && !RegExp(r'^[a-zA-Z0-9\s]+$').hasMatch(value)) {
|
|
return 'Special characters are not allowed';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final List<TextInputFormatter> inputFormatters = [];
|
|
|
|
if (isPreview) {
|
|
inputFormatters.add(
|
|
TextInputFormatter.withFunction((oldValue, newValue) => oldValue),
|
|
);
|
|
} else {
|
|
if (isMobileNumber) {
|
|
inputFormatters.add(FilteringTextInputFormatter.digitsOnly);
|
|
inputFormatters.add(LengthLimitingTextInputFormatter(mobileLength));
|
|
} else {
|
|
if (numbersOnly) {
|
|
inputFormatters.add(FilteringTextInputFormatter.digitsOnly);
|
|
}
|
|
if (onlyLetters) {
|
|
inputFormatters.add(
|
|
FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z\s]')),
|
|
);
|
|
}
|
|
if (noSpecialCharacters) {
|
|
inputFormatters.add(
|
|
FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9\s]')),
|
|
);
|
|
}
|
|
if (noSpace) {
|
|
inputFormatters.add(FilteringTextInputFormatter.deny(RegExp(r'\s')));
|
|
}
|
|
if (maxLength != null) {
|
|
inputFormatters.add(LengthLimitingTextInputFormatter(maxLength));
|
|
}
|
|
}
|
|
}
|
|
|
|
final fillColor = isPreview
|
|
? Colors.grey.shade100
|
|
: enabled
|
|
? const Color(0xFFFFF5F5)
|
|
: Colors.grey.shade200;
|
|
|
|
final borderColor = const Color(0xBBC83B61).withOpacity(0.4);
|
|
|
|
// ✅ Full radius used for normal fields
|
|
// ✅ Only right-side radius when prefix is present (left side is the prefix container)
|
|
final borderRadius = prefixWidget != null
|
|
? BorderRadius.only(
|
|
topRight: Radius.circular(8.r),
|
|
bottomRight: Radius.circular(8.r),
|
|
)
|
|
: BorderRadius.circular(8.r);
|
|
|
|
return Padding(
|
|
padding: EdgeInsets.only(bottom: 14.h),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Label
|
|
if (label.isNotEmpty) ...[
|
|
CustomText(text: label, size: 14.sp),
|
|
SizedBox(height: 6.h),
|
|
],
|
|
|
|
if (prefixWidget != null)
|
|
// ✅ THE CORE FIX:
|
|
// We split the phone field into two parts:
|
|
// 1. The input ROW (prefix + text field) — wrapped in IntrinsicHeight
|
|
// so both sides match height perfectly
|
|
// 2. The error text — rendered OUTSIDE and BELOW the row
|
|
// so IntrinsicHeight is never affected by error text height
|
|
_PrefixFieldWithError(
|
|
prefixWidget: prefixWidget!,
|
|
fillColor: fillColor,
|
|
borderColor: borderColor,
|
|
borderRadius: borderRadius,
|
|
maxLines: maxLines,
|
|
obscureText: obscureText,
|
|
enabled: isPreview ? false : enabled,
|
|
controller: controller,
|
|
validator: validator ?? _internalValidator,
|
|
keyboardType: keyboardType ?? TextInputType.phone,
|
|
inputFormatters: inputFormatters,
|
|
hint: hint,
|
|
onChanged: (value) {
|
|
if (isFirstLetterCapital) _capitalizeFirstLetter(value);
|
|
if (onChanged != null) onChanged!(value);
|
|
},
|
|
suffixIcon: suffixIcon,
|
|
)
|
|
else
|
|
TextFormField(
|
|
controller: controller,
|
|
maxLines: obscureText ? 1 : maxLines,
|
|
enabled: isPreview ? false : enabled,
|
|
obscureText: obscureText,
|
|
validator: validator ?? _internalValidator,
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
keyboardType: keyboardType ??
|
|
(isMobileNumber
|
|
? TextInputType.phone
|
|
: isEmail
|
|
? TextInputType.emailAddress
|
|
: TextInputType.name),
|
|
inputFormatters: inputFormatters,
|
|
onChanged: (value) {
|
|
if (isFirstLetterCapital) _capitalizeFirstLetter(value);
|
|
if (onChanged != null) onChanged!(value);
|
|
},
|
|
decoration: InputDecoration(
|
|
hintText: hint,
|
|
counterText: "",
|
|
hintStyle: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: const Color(0xFF8E8E8E),
|
|
),
|
|
filled: true,
|
|
fillColor: fillColor,
|
|
contentPadding: EdgeInsets.symmetric(
|
|
horizontal: 24.w,
|
|
vertical: maxLines != null && maxLines! > 1 ? 12.h : 10.h,
|
|
),
|
|
suffixIcon: suffixIcon,
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: borderRadius,
|
|
borderSide: BorderSide(color: borderColor, width: .4.w),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: borderRadius,
|
|
borderSide: BorderSide(
|
|
color: const Color(0xFFF95F62), width: 1.w),
|
|
),
|
|
errorBorder: OutlineInputBorder(
|
|
borderRadius: borderRadius,
|
|
borderSide: BorderSide(color: Colors.red, width: 1.w),
|
|
),
|
|
focusedErrorBorder: OutlineInputBorder(
|
|
borderRadius: borderRadius,
|
|
borderSide: BorderSide(color: Colors.red, width: 1.5.w),
|
|
),
|
|
errorStyle: TextStyle(
|
|
fontSize: 11.sp,
|
|
color: Colors.red,
|
|
height: 1.3,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// ✅ Separate StatefulWidget for the prefix + field combo.
|
|
/// It manually manages validation state and renders error text
|
|
/// OUTSIDE the IntrinsicHeight row — this is the key to perfect alignment.
|
|
class _PrefixFieldWithError extends StatefulWidget {
|
|
final Widget prefixWidget;
|
|
final Color fillColor;
|
|
final Color borderColor;
|
|
final BorderRadius borderRadius;
|
|
final int? maxLines;
|
|
final bool obscureText;
|
|
final bool enabled;
|
|
final TextEditingController controller;
|
|
final String? Function(String?)? validator;
|
|
final TextInputType keyboardType;
|
|
final List<TextInputFormatter> inputFormatters;
|
|
final String hint;
|
|
final void Function(String) onChanged;
|
|
final Widget? suffixIcon;
|
|
|
|
const _PrefixFieldWithError({
|
|
required this.prefixWidget,
|
|
required this.fillColor,
|
|
required this.borderColor,
|
|
required this.borderRadius,
|
|
required this.maxLines,
|
|
required this.obscureText,
|
|
required this.enabled,
|
|
required this.controller,
|
|
required this.validator,
|
|
required this.keyboardType,
|
|
required this.inputFormatters,
|
|
required this.hint,
|
|
required this.onChanged,
|
|
required this.suffixIcon,
|
|
});
|
|
|
|
@override
|
|
State<_PrefixFieldWithError> createState() => _PrefixFieldWithErrorState();
|
|
}
|
|
|
|
class _PrefixFieldWithErrorState extends State<_PrefixFieldWithError> {
|
|
String? _errorText;
|
|
bool _hasInteracted = false;
|
|
|
|
void _validate(String value) {
|
|
if (!_hasInteracted) return;
|
|
setState(() {
|
|
_errorText = widget.validator?.call(value);
|
|
});
|
|
}
|
|
|
|
void _onChanged(String value) {
|
|
setState(() => _hasInteracted = true);
|
|
_validate(value);
|
|
widget.onChanged(value);
|
|
}
|
|
|
|
// Called by Form.validate() via FormField
|
|
String? _formValidator(String? value) {
|
|
setState(() => _hasInteracted = true);
|
|
final error = widget.validator?.call(value);
|
|
// Update error text after frame
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) setState(() => _errorText = error);
|
|
});
|
|
return error;
|
|
}
|
|
|
|
bool get _hasError => _errorText != null && _errorText!.isNotEmpty;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final borderColor = widget.borderColor;
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// ✅ IntrinsicHeight ONLY wraps the input row (prefix + field)
|
|
// Error text is outside this, so IntrinsicHeight height is never affected by it
|
|
IntrinsicHeight(
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
// Prefix container — matches field height perfectly via IntrinsicHeight
|
|
Container(
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: widget.fillColor,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(8.r),
|
|
bottomLeft: Radius.circular(8.r),
|
|
),
|
|
// ✅ No right border — avoids double border line
|
|
border: Border(
|
|
top: BorderSide(
|
|
color: _hasError ? Colors.red : borderColor,
|
|
width: _hasError ? 1.w : 0.4.w,
|
|
),
|
|
bottom: BorderSide(
|
|
color: _hasError ? Colors.red : borderColor,
|
|
width: _hasError ? 1.w : 0.4.w,
|
|
),
|
|
left: BorderSide(
|
|
color: _hasError ? Colors.red : borderColor,
|
|
width: _hasError ? 1.w : 0.4.w,
|
|
),
|
|
),
|
|
),
|
|
child: widget.prefixWidget,
|
|
),
|
|
|
|
// Text field — takes remaining width
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: widget.controller,
|
|
maxLines: widget.obscureText ? 1 : widget.maxLines,
|
|
enabled: widget.enabled,
|
|
obscureText: widget.obscureText,
|
|
validator: _formValidator,
|
|
// ✅ No autovalidateMode here — we handle it manually
|
|
// so we can show error text outside the row
|
|
autovalidateMode: AutovalidateMode.disabled,
|
|
keyboardType: widget.keyboardType,
|
|
inputFormatters: widget.inputFormatters,
|
|
onChanged: _onChanged,
|
|
decoration: InputDecoration(
|
|
hintText: widget.hint,
|
|
counterText: "",
|
|
// ✅ errorText: null always — we render error ourselves below
|
|
errorText: null,
|
|
hintStyle: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: const Color(0xFF8E8E8E),
|
|
),
|
|
filled: true,
|
|
fillColor: widget.fillColor,
|
|
contentPadding: EdgeInsets.symmetric(
|
|
horizontal: 12.w,
|
|
vertical: 10.h,
|
|
),
|
|
suffixIcon: widget.suffixIcon,
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: widget.borderRadius,
|
|
borderSide: BorderSide(
|
|
color: _hasError ? Colors.red : borderColor,
|
|
width: _hasError ? 1.w : 0.4.w,
|
|
),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: widget.borderRadius,
|
|
borderSide: BorderSide(
|
|
color: _hasError
|
|
? Colors.red
|
|
: const Color(0xFFF95F62),
|
|
width: 1.w,
|
|
),
|
|
),
|
|
errorBorder: OutlineInputBorder(
|
|
borderRadius: widget.borderRadius,
|
|
borderSide:
|
|
BorderSide(color: Colors.red, width: 1.w),
|
|
),
|
|
focusedErrorBorder: OutlineInputBorder(
|
|
borderRadius: widget.borderRadius,
|
|
borderSide:
|
|
BorderSide(color: Colors.red, width: 1.5.w),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
// ✅ Error text rendered OUTSIDE the IntrinsicHeight row
|
|
// This is why the prefix box never grows when error appears
|
|
if (_hasError) ...[
|
|
SizedBox(height: 4.h),
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 4.w),
|
|
child: Text(
|
|
_errorText!,
|
|
style: TextStyle(
|
|
fontSize: 11.sp,
|
|
color: Colors.red,
|
|
height: 1.3,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
} |