279 lines
8.0 KiB
Dart
279 lines
8.0 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; // ✅ NEW: optional prefix (e.g. CountryCodePicker)
|
|
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, // ✅ NEW
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
// ✅ Determine border radius — if prefixWidget is present, only round the right side
|
|
final borderRadius = prefixWidget != null
|
|
? BorderRadius.only(
|
|
topRight: Radius.circular(8.r),
|
|
bottomRight: Radius.circular(8.r),
|
|
)
|
|
: BorderRadius.circular(8.r);
|
|
|
|
// ✅ Determine fill color
|
|
final fillColor = isPreview
|
|
? Colors.grey.shade100
|
|
: enabled
|
|
? const Color(0xFFFFF5F5)
|
|
: Colors.grey.shade200;
|
|
|
|
final textFormField = 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(
|
|
// ✅ Reduce left padding when prefixWidget takes up the left side
|
|
horizontal: prefixWidget != null ? 12.w : 24.w,
|
|
vertical: maxLines != null && maxLines! > 1 ? 12.h : 10.h,
|
|
),
|
|
suffixIcon: suffixIcon,
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: borderRadius,
|
|
borderSide: BorderSide(
|
|
color: const Color(0xBBC83B61).withOpacity(0.4),
|
|
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,
|
|
),
|
|
),
|
|
);
|
|
|
|
return Padding(
|
|
padding: EdgeInsets.only(bottom: 14.h),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// ✅ Only show label row if label is not empty
|
|
if (label.isNotEmpty) ...[
|
|
CustomText(text: label, size: 14.sp),
|
|
SizedBox(height: 6.h),
|
|
],
|
|
|
|
// ✅ If prefixWidget provided, wrap it in a Row with the picker on the left
|
|
if (prefixWidget != null)
|
|
IntrinsicHeight(
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
// Prefix container — styled to match the field
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: fillColor,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(8.r),
|
|
bottomLeft: Radius.circular(8.r),
|
|
),
|
|
border: Border.all(
|
|
color: const Color(0xBBC83B61).withOpacity(0.4),
|
|
width: 0.4.w,
|
|
),
|
|
),
|
|
child: prefixWidget!,
|
|
),
|
|
// TextField takes the remaining space
|
|
Expanded(child: textFormField),
|
|
],
|
|
),
|
|
)
|
|
else
|
|
textFormField,
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |