// ignore_for_file: must_be_immutable import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:intl/intl.dart'; import '../themes.dart'; class AppTextField extends StatefulWidget { final bool separarMiles; final Icon? prefixIcon; final Widget? suffixIcon; final String? labelText; final String? initialValue; final String? hintText; final TextInputType? keyboardType; final TextEditingController? controller; final Color? fillColor; final void Function()? onTap; final bool enabled; final bool obscureText; final bool readOnly; final int? maxLength; final int? maxLines; final String? errorText; final double? textfieldHeight; final List? autofillHints; List? inputFormatters; final TextCapitalization textCapitalization; String? etiqueta; String? Function(String?)? validator; final double? vertical; final Function(String v)? onChanged; final Function(String v)? onSubmitted; AppTextField({ super.key, this.separarMiles = false, this.etiqueta, this.labelText, this.prefixIcon, this.keyboardType, this.onTap, this.controller, this.hintText, this.suffixIcon, this.inputFormatters, this.enabled = true, this.readOnly = false, this.obscureText = false, this.fillColor = Colors.white, this.errorText, this.textfieldHeight, this.textCapitalization = TextCapitalization.none, this.initialValue, this.maxLength, this.maxLines = 1, this.validator, this.onChanged, this.vertical, this.autofillHints, this.onSubmitted, }); @override _AppTextFieldState createState() => _AppTextFieldState(); } class _AppTextFieldState extends State { bool isFirst = true; double _getFontSize(BuildContext context) { double screenWidth = MediaQuery.of(context).size.width; if (screenWidth < 480) { return 12; } else if (screenWidth < 800) { return 15; } return 18; } @override Widget build(BuildContext context) { double fontSize = _getFontSize(context); widget.etiqueta ??= ""; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (widget.etiqueta != '') Text( widget.etiqueta!, style: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: widget.enabled ? Colors.black : Colors.grey[700]), ), if (widget.etiqueta != '') const SizedBox( height: 5, ), TextFormField( onFieldSubmitted: widget.onSubmitted, autofillHints: widget.autofillHints, validator: widget.validator, enabled: widget.enabled, style: TextStyle( fontSize: fontSize, ), maxLength: widget.maxLength, maxLines: widget.maxLines, inputFormatters: widget.inputFormatters ?? (widget.separarMiles ? [FilteringTextInputFormatter.digitsOnly] : null), initialValue: widget.initialValue, controller: widget.controller, onTap: widget.onTap, readOnly: widget.readOnly, keyboardType: widget.keyboardType, textCapitalization: widget.textCapitalization, cursorColor: AppTheme.tertiary, obscureText: widget.obscureText, autocorrect: true, onChanged: (value) { if (widget.separarMiles) { String newValue = value.replaceAll(',', '').replaceAll('.', ''); if (value.isEmpty || newValue == '00') { widget.controller?.clear(); isFirst = true; return; } double value1 = double.parse(newValue); if (!isFirst) value1 = value1 * 100; value = NumberFormat.currency(customPattern: '###,###.##') .format(value1 / 100); widget.controller?.value = TextEditingValue( text: value, selection: TextSelection.collapsed(offset: value.length), ); } if (widget.onChanged != null) { widget.onChanged!(value); } }, decoration: InputDecoration( contentPadding: widget.vertical == null ? null : EdgeInsets.symmetric(vertical: widget.vertical!), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide( color: AppTheme.tertiary, ), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide( color: Colors.grey[400]!, ), ), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide( color: Colors.red[200]!, ), ), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide( color: Colors.red, ), ), disabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide( color: Colors.grey[200]!, ), ), errorText: widget.errorText, labelText: widget.labelText, hintText: widget.hintText, floatingLabelStyle: TextStyle( color: AppTheme.tertiary, fontSize: fontSize, ), filled: true, fillColor: widget.fillColor, prefixIcon: widget.prefixIcon, prefixIconColor: AppTheme.tertiary, suffixIcon: widget.suffixIcon, suffixIconColor: AppTheme.tertiary, border: OutlineInputBorder( borderRadius: BorderRadius.circular(15), borderSide: BorderSide.none, ), ), ), ], ); } }