Advertisement
rifki_cs29

CustomTextField

May 9th, 2023
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.58 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:module_core/module_core.dart';
  4. import 'package:module_dependencies/module_dependencies_ui.dart';
  5.  
  6. class CustomTextField extends StatefulWidget {
  7.   const CustomTextField({
  8.     Key? key,
  9.     this.controller,
  10.     this.textName,
  11.     this.labelText,
  12.     this.label,
  13.     this.hintText,
  14.     this.prefix,
  15.     this.prefixIcon,
  16.     this.suffixIcon,
  17.     this.keyboardType,
  18.     this.validator,
  19.     this.inputFormatters,
  20.     this.onChanged,
  21.     this.onEditingComplete,
  22.     this.onFieldSubmitted,
  23.     this.textAlign = TextAlign.start,
  24.     this.style,
  25.     this.maxLength,
  26.     this.minLines,
  27.     this.maxLines,
  28.     this.underlined = true,
  29.     this.onTap,
  30.     this.readOnly = false,
  31.     this.fillColor,
  32.     this.textInputAction,
  33.     this.isObsecure = false,
  34.   }) : super(key: key);
  35.  
  36.   final TextEditingController? controller;
  37.   final String? textName;
  38.   final Color? fillColor;
  39.   final String? labelText;
  40.   final Widget? label;
  41.   final String? hintText;
  42.   final Widget? prefix;
  43.   final Widget? prefixIcon;
  44.   final Widget? suffixIcon;
  45.   final TextInputType? keyboardType;
  46.   final String? Function(String?)? validator;
  47.   final List<TextInputFormatter>? inputFormatters;
  48.   final void Function(String)? onChanged;
  49.   final void Function()? onEditingComplete;
  50.   final void Function(String)? onFieldSubmitted;
  51.   final TextAlign textAlign;
  52.   final TextStyle? style;
  53.   final int? maxLength;
  54.   final int? minLines;
  55.   final int? maxLines;
  56.   final bool underlined;
  57.   final void Function()? onTap;
  58.   final bool readOnly;
  59.   final TextInputAction? textInputAction;
  60.   final bool isObsecure;
  61.  
  62.   @override
  63.   State<CustomTextField> createState() => _CustomTextFieldState();
  64. }
  65.  
  66. class _CustomTextFieldState extends State<CustomTextField> {
  67.   OutlineInputBorder get _border => OutlineInputBorder(
  68.         borderRadius: const BorderRadius.all(Radius.circular(12)),
  69.         borderSide: BorderSide(
  70.           width: 1.5,
  71.           color:
  72.               widget.underlined ? const Color(0xFFFF7226) : Colors.transparent,
  73.         ),
  74.       );
  75.  
  76.   OutlineInputBorder get _enabledBorder => OutlineInputBorder(
  77.         borderRadius: const BorderRadius.all(Radius.circular(12)),
  78.         borderSide: BorderSide(
  79.           width: 1.5,
  80.           color: widget.underlined ? colorBlack60 : Colors.transparent,
  81.         ),
  82.       );
  83.  
  84.   OutlineInputBorder get _errorBorder => OutlineInputBorder(
  85.         borderRadius: const BorderRadius.all(Radius.circular(12)),
  86.         borderSide: BorderSide(
  87.           width: 1.5,
  88.           color: widget.underlined ? colorDanger : Colors.transparent,
  89.         ),
  90.       );
  91.  
  92.   Widget eyeHiddenIcon = SizedBox(
  93.     width: 20,
  94.     height: 20,
  95.     child: SvgPicture.asset(
  96.       Assets.svg.eyeHide,
  97.       fit: BoxFit.fitWidth,
  98.     ),
  99.   );
  100.  
  101.   Widget eyeShowIcon = SizedBox(
  102.     height: 20,
  103.     width: 20,
  104.     child: SvgPicture.asset(
  105.       Assets.svg.eyeShow,
  106.       fit: BoxFit.fitWidth,
  107.     ),
  108.   );
  109.  
  110.   bool obsureText = false;
  111.  
  112.   @override
  113.   void initState() {
  114.     super.initState();
  115.     setState(() {
  116.       if (widget.isObsecure == true) {
  117.         obsureText = true;
  118.       }
  119.     });
  120.   }
  121.  
  122.   @override
  123.   Widget build(BuildContext context) {
  124.     return Column(
  125.       crossAxisAlignment: CrossAxisAlignment.start,
  126.       children: [
  127.         if (widget.textName != null)
  128.           Padding(
  129.             padding: const EdgeInsets.only(left: 16.0),
  130.             child: Text(
  131.               widget.textName ?? 'textName',
  132.               style: GoogleFonts.inter(
  133.                 fontSize: 12.sp,
  134.                 fontWeight: FontWeight.w400,
  135.                 color: const Color(0xFF000000),
  136.               ),
  137.             ),
  138.           ),
  139.         if (widget.textName != null) const Gap(8),
  140.         ClipRRect(
  141.           borderRadius: const BorderRadius.all(Radius.circular(8)),
  142.           child: TextFormField(
  143.             validator: widget.validator,
  144.             keyboardType: widget.keyboardType,
  145.             maxLength: widget.maxLength,
  146.             maxLines: widget.isObsecure == true ? 1 : widget.maxLines,
  147.             minLines: widget.minLines,
  148.             style: widget.style ??
  149.                 GoogleFonts.inter(
  150.                   fontSize: 14,
  151.                   fontWeight: FontWeight.w400,
  152.                   color: widget.readOnly == true
  153.                       ? Colors.grey
  154.                       : const Color(0xFF2D2F2E),
  155.                 ),
  156.             obscureText: obsureText,
  157.             textAlign: widget.textAlign,
  158.             cursorColor: const Color(0xFF2D2F2E),
  159.             controller: widget.controller,
  160.             inputFormatters: widget.inputFormatters,
  161.             onChanged: widget.onChanged,
  162.             onTap: widget.onTap,
  163.             readOnly: widget.readOnly,
  164.             onEditingComplete: widget.onEditingComplete,
  165.             onFieldSubmitted: widget.onFieldSubmitted,
  166.             textInputAction: widget.textInputAction,
  167.             textAlignVertical:
  168.                 (widget.prefixIcon != null || widget.suffixIcon != null)
  169.                     ? TextAlignVertical.center
  170.                     : null,
  171.             decoration: InputDecoration(
  172.               labelText: widget.labelText,
  173.               hintText: widget.hintText,
  174.               label: widget.label,
  175.               prefix: widget.prefix,
  176.               prefixIcon: widget.prefixIcon,
  177.               suffixIcon: (widget.isObsecure == true)
  178.                   ? IconButton(
  179.                       splashRadius: 1,
  180.                       icon: (obsureText == true) ? eyeShowIcon : eyeHiddenIcon,
  181.                       onPressed: () {
  182.                         setState(() {
  183.                           obsureText = !obsureText;
  184.                         });
  185.                       },
  186.                     )
  187.                   : widget.suffixIcon,
  188.               labelStyle: GoogleFonts.inter(
  189.                 fontSize: 14,
  190.                 fontWeight: FontWeight.w400,
  191.                 color: const Color(0xFF2D2F2E),
  192.               ),
  193.               hintStyle: GoogleFonts.inter(
  194.                 fontSize: 14,
  195.                 fontWeight: FontWeight.w400,
  196.                 color: const Color(0xFFC4C4C4),
  197.                 fontStyle: FontStyle.italic,
  198.               ),
  199.               filled: true,
  200.               isDense: true,
  201.               fillColor: widget.fillColor ?? const Color(0xFFFFFFFF),
  202.               focusedBorder: _border,
  203.               enabledBorder: _enabledBorder,
  204.               border: _border,
  205.               errorBorder: _errorBorder,
  206.             ),
  207.           ),
  208.         ),
  209.       ],
  210.     );
  211.   }
  212. }
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement