127 lines
4.0 KiB
Dart
127 lines
4.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 void Function(String)? onChanged;
|
|
|
|
// ✅ NEW
|
|
final int? maxLength; // e.g. 10
|
|
final bool numbersOnly; // allow only digits
|
|
|
|
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,
|
|
|
|
// ✅ NEW
|
|
this.maxLength, // default = null (infinite)
|
|
this.numbersOnly = false, // default = false
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: EdgeInsets.only(bottom: 12.h),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
CustomText(
|
|
text: label,
|
|
size: 14.sp,
|
|
),
|
|
SizedBox(height: 6.h),
|
|
SizedBox(
|
|
height: maxLines == 1 ? 42.h : null,
|
|
child: TextFormField(
|
|
controller: controller,
|
|
maxLines: obscureText ? 1 : maxLines,
|
|
enabled: enabled,
|
|
validator: validator,
|
|
keyboardType: keyboardType,
|
|
obscureText: obscureText,
|
|
onChanged: onChanged,
|
|
|
|
// ✅ NEW
|
|
maxLength: maxLength,
|
|
inputFormatters: [
|
|
if (numbersOnly)
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
if (maxLength != null)
|
|
LengthLimitingTextInputFormatter(maxLength),
|
|
],
|
|
|
|
decoration: InputDecoration(
|
|
hintText: hint,
|
|
counterText: "", // ✅ hides 0/10 counter
|
|
hintStyle: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: const Color(0xFF8E8E8E),
|
|
),
|
|
filled: true,
|
|
fillColor: enabled
|
|
? const Color(0xFFFFF5F5)
|
|
: Colors.grey.shade200,
|
|
contentPadding: EdgeInsets.symmetric(
|
|
horizontal: 24.w,
|
|
vertical: maxLines != null && maxLines! > 1 ? 12.h : 0,
|
|
),
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |