| 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:flutter/services.dart'; |
| 3 | |
| 4 | class BaseTextFormField extends StatelessWidget { |
| 5 | BaseTextFormField({ |
| 6 | this.onChanged, |
| 7 | this.controller, |
| 8 | this.keyboardType = TextInputType.text, |
| 9 | this.textInputAction = TextInputAction.done, |
| 10 | this.textAlign = TextAlign.start, |
| 11 | this.autovalidateMode, |
| 12 | this.hintText = '', |
| 13 | this.maxLines = 1, |
| 14 | this.inputFormatters, |
| 15 | this.textColor, |
| 16 | this.hintColor, |
| 17 | this.fillColor, |
| 18 | this.prefix, |
| 19 | this.prefixIcon, |
| 20 | this.suffix, |
| 21 | this.suffixIcon, |
| 22 | this.enabled = true, |
| 23 | this.readOnly = false, |
| 24 | this.enableInteractiveSelection = true, |
| 25 | this.obscureText = false, |
| 26 | this.validator, |
| 27 | this.textStyle, |
| 28 | this.placeholderTextStyle, |
| 29 | this.maxLength, |
| 30 | this.focusNode, |
| 31 | this.initialValue, |
| 32 | this.onSubmit, |
| 33 | this.autofocus = false, |
| 34 | this.autocorrect = true, |
| 35 | this.enableSuggestions = true, |
| 36 | this.contentPadding, |
| 37 | this.alignLabelWithHint = false, |
| 38 | this.floatingLabelBehavior, |
| 39 | this.cursorColor, |
| 40 | this.cursorWidth, |
| 41 | this.isDense, |
| 42 | this.enableIMEPersonalizedLearning, |
| 43 | this.onFieldSubmitted, |
| 44 | this.hasUnderlineBorder = false, |
| 45 | this.borderWidth = 1.0, |
| 46 | this.prefixIconConstraints, |
| 47 | this.suffixIconConstraints, |
| 48 | super.key, |
| 49 | this.suffixText, |
| 50 | this.borderRadius = const BorderRadius.all(Radius.circular(18)), |
| 51 | this.onEditingComplete, |
| 52 | }); |
| 53 | |
| 54 | final TextEditingController? controller; |
| 55 | final TextInputType? keyboardType; |
| 56 | final TextInputAction? textInputAction; |
| 57 | final TextAlign textAlign; |
| 58 | final AutovalidateMode? autovalidateMode; |
| 59 | final String? hintText; |
| 60 | final int? maxLines; |
| 61 | final List<TextInputFormatter>? inputFormatters; |
| 62 | final Color? textColor; |
| 63 | final Color? hintColor; |
| 64 | final Color? fillColor; |
| 65 | final Widget? prefix; |
| 66 | final Widget? prefixIcon; |
| 67 | final Widget? suffix; |
| 68 | final Widget? suffixIcon; |
| 69 | final bool? enabled; |
| 70 | final FormFieldValidator<String>? validator; |
| 71 | final TextStyle? placeholderTextStyle; |
| 72 | final TextStyle? textStyle; |
| 73 | final int? maxLength; |
| 74 | final FocusNode? focusNode; |
| 75 | final bool readOnly; |
| 76 | final bool? enableInteractiveSelection; |
| 77 | final String? initialValue; |
| 78 | final BoxConstraints? prefixIconConstraints; |
| 79 | final BoxConstraints? suffixIconConstraints; |
| 80 | final String? suffixText; |
| 81 | final void Function(String)? onSubmit; |
| 82 | final bool obscureText; |
| 83 | final bool? autofocus; |
| 84 | final bool? autocorrect; |
| 85 | final bool? enableSuggestions; |
| 86 | final void Function(String)? onChanged; |
| 87 | final VoidCallback? onEditingComplete; |
| 88 | final EdgeInsetsGeometry? contentPadding; |
| 89 | final bool? alignLabelWithHint; |
| 90 | final FloatingLabelBehavior? floatingLabelBehavior; |
| 91 | final Color? cursorColor; |
| 92 | final double? cursorWidth; |
| 93 | final bool? isDense; |
| 94 | final bool? enableIMEPersonalizedLearning; |
| 95 | final void Function(String)? onFieldSubmitted; |
| 96 | final bool hasUnderlineBorder; |
| 97 | final double borderWidth; |
| 98 | final BorderRadius borderRadius; |
| 99 | |
| 100 | @override |
| 101 | Widget build(BuildContext context) { |
| 102 | return TextFormField( |
| 103 | enableIMEPersonalizedLearning: enableIMEPersonalizedLearning ?? true, |
| 104 | cursorColor: cursorColor, |
| 105 | cursorWidth: cursorWidth ?? 2.0, |
| 106 | onChanged: onChanged, |
| 107 | autofocus: autofocus ?? false, |
| 108 | autocorrect: autocorrect ?? true, |
| 109 | enableSuggestions: enableSuggestions ?? true, |
| 110 | enableInteractiveSelection: enableInteractiveSelection, |
| 111 | readOnly: readOnly, |
| 112 | initialValue: initialValue, |
| 113 | focusNode: focusNode, |
| 114 | controller: controller, |
| 115 | keyboardType: keyboardType, |
| 116 | textInputAction: textInputAction, |
| 117 | onEditingComplete: onEditingComplete, |
| 118 | textAlign: textAlign, |
| 119 | autovalidateMode: autovalidateMode, |
| 120 | obscureText: obscureText, |
| 121 | maxLines: maxLines, |
| 122 | inputFormatters: inputFormatters, |
| 123 | enabled: enabled, |
| 124 | maxLength: maxLength, |
| 125 | onFieldSubmitted: onFieldSubmitted ?? onSubmit, |
| 126 | style: textStyle ?? |
| 127 | Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 128 | fontSize: 16.0, color: textColor ?? Theme.of(context).colorScheme.onSurface), |
| 129 | decoration: InputDecoration( |
| 130 | isDense: isDense, |
| 131 | alignLabelWithHint: alignLabelWithHint, |
| 132 | contentPadding: contentPadding, |
| 133 | floatingLabelBehavior: floatingLabelBehavior ?? FloatingLabelBehavior.never, |
| 134 | prefixIconConstraints: |
| 135 | prefixIconConstraints ?? const BoxConstraints(minWidth: 40, minHeight: 0), |
| 136 | prefix: prefix, |
| 137 | prefixIcon: prefixIcon, |
| 138 | suffixIconConstraints: |
| 139 | suffixIconConstraints ?? const BoxConstraints(minWidth: 0, minHeight: 0), |
| 140 | suffix: suffix, |
| 141 | suffixIcon: suffixIcon, |
| 142 | filled: !hasUnderlineBorder, |
| 143 | fillColor: |
| 144 | hasUnderlineBorder ? null : fillColor ?? Theme.of(context).colorScheme.surfaceContainer, |
| 145 | hintStyle: placeholderTextStyle ?? |
| 146 | Theme.of(context) |
| 147 | .textTheme |
| 148 | .bodyMedium! |
| 149 | .copyWith(color: hintColor ?? Theme.of(context).hintColor, fontSize: 16), |
| 150 | hintText: hasUnderlineBorder ? hintText : null, |
| 151 | labelText: !hasUnderlineBorder ? hintText : null, |
| 152 | labelStyle: !hasUnderlineBorder ? placeholderTextStyle : null, |
| 153 | border: hasUnderlineBorder |
| 154 | ? UnderlineInputBorder( |
| 155 | borderRadius: borderRadius, |
| 156 | borderSide: BorderSide( |
| 157 | color: Theme.of(context).colorScheme.outlineVariant, |
| 158 | style: borderWidth == 0.0 ? BorderStyle.none : BorderStyle.solid, |
| 159 | width: borderWidth, |
| 160 | ), |
| 161 | ) |
| 162 | : OutlineInputBorder(borderRadius: borderRadius, borderSide: BorderSide.none), |
| 163 | focusedBorder: hasUnderlineBorder |
| 164 | ? UnderlineInputBorder( |
| 165 | borderRadius: borderRadius, |
| 166 | borderSide: BorderSide( |
| 167 | color: Theme.of(context).colorScheme.outlineVariant, |
| 168 | style: borderWidth == 0.0 ? BorderStyle.none : BorderStyle.solid, |
| 169 | width: borderWidth, |
| 170 | ), |
| 171 | ) |
| 172 | : OutlineInputBorder(borderRadius: borderRadius, borderSide: BorderSide.none), |
| 173 | disabledBorder: hasUnderlineBorder |
| 174 | ? UnderlineInputBorder( |
| 175 | borderRadius: borderRadius, |
| 176 | borderSide: BorderSide( |
| 177 | color: Theme.of(context).colorScheme.outlineVariant, |
| 178 | width: borderWidth, |
| 179 | ), |
| 180 | ) |
| 181 | : OutlineInputBorder(borderRadius: borderRadius, borderSide: BorderSide.none), |
| 182 | enabledBorder: hasUnderlineBorder |
| 183 | ? UnderlineInputBorder( |
| 184 | borderRadius: borderRadius, |
| 185 | borderSide: BorderSide( |
| 186 | color: Theme.of(context).colorScheme.outlineVariant, |
| 187 | style: borderWidth == 0.0 ? BorderStyle.none : BorderStyle.solid, |
| 188 | width: borderWidth, |
| 189 | ), |
| 190 | ) |
| 191 | : OutlineInputBorder(borderRadius: borderRadius, borderSide: BorderSide.none), |
| 192 | errorBorder: hasUnderlineBorder |
| 193 | ? UnderlineInputBorder( |
| 194 | borderRadius: borderRadius, |
| 195 | borderSide: BorderSide(color: Theme.of(context).colorScheme.error), |
| 196 | ) |
| 197 | : OutlineInputBorder(borderRadius: borderRadius, borderSide: BorderSide.none), |
| 198 | ), |
| 199 | validator: validator, |
| 200 | ); |
| 201 | } |
| 202 | } |