Files
CityCards_Customer_Flutter/lib/common_packages/custom_textfield.dart

236 lines
6.9 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 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; // ✅ NEW
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.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, // ✅ NEW
});
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; // ✅ Skip validation in preview mode
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 = [];
// ✅ Block all input in preview mode
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));
}
}
}
return Padding(
padding: EdgeInsets.only(bottom: 14.h),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CustomText(
text: label,
size: 14.sp,
),
SizedBox(height: 6.h),
TextFormField(
controller: controller,
maxLines: obscureText ? 1 : maxLines,
enabled: isPreview ? false : enabled, // ✅ Disable in preview
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: isPreview
? Colors.grey.shade100 // ✅ Distinct preview background
: enabled
? const Color(0xFFFFF5F5)
: Colors.grey.shade200,
contentPadding: EdgeInsets.symmetric(
horizontal: 24.w,
vertical: maxLines != null && maxLines! > 1 ? 12.h : 10.h,
),
suffixIcon: suffixIcon,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.r),
borderSide: BorderSide(
color: const Color(0xBBC83B61).withOpacity(0.4),
width: .4.w,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.r),
borderSide: BorderSide(
color: const Color(0xFFF95F62),
width: 1.w,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.r),
borderSide: BorderSide(
color: Colors.red,
width: 1.w,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.r),
borderSide: BorderSide(
color: Colors.red,
width: 1.5.w,
),
),
errorStyle: TextStyle(
fontSize: 11.sp,
color: Colors.red,
height: 1.3,
),
),
),
],
),
);
}
}