You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

179 line
6.5 KiB

  1. // ignore_for_file: public_member_api_docs, sort_constructors_first
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:qadirneyriz/config/config.dart';
  5. import 'package:qadirneyriz/widgets/custom_button.dart';
  6. // ignore: must_be_immutable
  7. class CustomTextField extends StatefulWidget {
  8. final String label;
  9. final String hintText;
  10. final bool obscureText;
  11. final double contentPadding;
  12. final bool hasButton;
  13. final TextInputAction textInputAction;
  14. final TextEditingController textEditingController;
  15. final TextInputType textInputType;
  16. final int? maxLine;
  17. final Color? fillsColor;
  18. bool isReadOnly = false;
  19. final double paddingVertical;
  20. final double paddingHarizon;
  21. bool isPass = false;
  22. FocusNode? focus;
  23. IconData? firstIcon;
  24. final void Function(String s)? onChanged;
  25. final void Function()? buttonOnPressed;
  26. bool hasLable = true;
  27. CustomTextField({
  28. Key? key,
  29. required this.label,
  30. required this.hintText,
  31. this.obscureText = false,
  32. this.contentPadding = 10,
  33. this.hasButton = false,
  34. this.textInputAction = TextInputAction.done,
  35. required this.textEditingController,
  36. required this.textInputType,
  37. this.maxLine,
  38. this.fillsColor = Colors.white,
  39. this.isReadOnly = false,
  40. this.isPass = false,
  41. this.focus,
  42. this.firstIcon,
  43. this.onChanged,
  44. this.buttonOnPressed,
  45. this.hasLable = true,
  46. this.paddingVertical = 10,
  47. this.paddingHarizon = 20,
  48. }) : super(key: key);
  49. @override
  50. _CustomTextFieldState createState() => _CustomTextFieldState();
  51. }
  52. bool obscureText = true;
  53. class _CustomTextFieldState extends State<CustomTextField> {
  54. @override
  55. Widget build(BuildContext context) {
  56. return Padding(
  57. padding: EdgeInsets.symmetric(
  58. horizontal: widget.paddingHarizon, vertical: widget.paddingVertical),
  59. child: Column(
  60. crossAxisAlignment: CrossAxisAlignment.start,
  61. children: [
  62. widget.hasLable
  63. ? Padding(
  64. padding: const EdgeInsets.only(right: 5),
  65. child: Text(
  66. widget.label,
  67. style: TextStyle(
  68. color: config.ui.mainGray.withOpacity(.7),
  69. fontSize: 15,
  70. ),
  71. ),
  72. )
  73. : Container(),
  74. SizedBox(height: widget.hasLable ? 6 : 0),
  75. Stack(
  76. children: [
  77. TextFormField(
  78. inputFormatters: [
  79. widget.textInputType == TextInputType.number
  80. ? FilteringTextInputFormatter.allow(
  81. RegExp('[a-z A-Z 0-9 ا-ی]'))
  82. : FilteringTextInputFormatter.singleLineFormatter
  83. ],
  84. readOnly: widget.isReadOnly,
  85. onFieldSubmitted: (v) {
  86. FocusScope.of(context).requestFocus(widget.focus);
  87. },
  88. onChanged: widget.onChanged,
  89. minLines: 1, // تعیین ارتفاع بر اساس تعداد خطوط
  90. maxLines: 1, // تعداد خطوط نامحدود
  91. controller: widget.textEditingController,
  92. keyboardType: widget.textInputType,
  93. textInputAction: widget.textInputAction,
  94. obscureText: (widget.isPass) ? obscureText : false,
  95. style: const TextStyle(color: Colors.black),
  96. decoration: InputDecoration(
  97. hintText: widget.hintText,
  98. hintStyle: TextStyle(
  99. color: config.ui.mainGray.withOpacity(.6),
  100. fontSize: 12),
  101. // filled: true,
  102. // fillColor: widget.fillsColor,
  103. enabledBorder: UnderlineInputBorder(
  104. borderSide: BorderSide(
  105. color:
  106. config.ui.mainGreen, // رنگ بردر پایین در حالت فعال
  107. width: 2, // ضخامت بردر پایین
  108. ),
  109. ),
  110. focusedBorder: UnderlineInputBorder(
  111. borderSide: BorderSide(
  112. color:
  113. config.ui.mainGreen, // رنگ بردر پایین در حالت فوکوس
  114. width: 2, // ضخامت بردر پایین
  115. ),
  116. ),
  117. prefixIcon: widget.firstIcon != null
  118. ? Row(
  119. mainAxisSize: MainAxisSize.min,
  120. mainAxisAlignment: MainAxisAlignment.center,
  121. children: [
  122. Icon(
  123. widget.firstIcon,
  124. color: config.ui.mainGreen,
  125. ),
  126. const SizedBox(
  127. width: 10,
  128. ),
  129. Container(
  130. color: config.ui.mainGreen,
  131. width: 1.5,
  132. height: 25,
  133. ),
  134. ],
  135. )
  136. : null,
  137. suffixIcon: (widget.isPass == true)
  138. ? InkWell(
  139. onTap: () {
  140. setState(() {
  141. obscureText = !obscureText;
  142. });
  143. },
  144. child: Icon(
  145. obscureText
  146. ? Icons.visibility_off
  147. : Icons.visibility,
  148. color: Colors.grey,
  149. ),
  150. )
  151. : null,
  152. contentPadding: EdgeInsets.all(widget.contentPadding)),
  153. ),
  154. widget.hasButton
  155. ? Positioned(
  156. left: 10,
  157. top: 10,
  158. child: Center(
  159. child: CustomButton(
  160. hieght: 35,
  161. width: 100,
  162. text: 'ثبت',
  163. onPressed: widget.buttonOnPressed,
  164. ),
  165. ))
  166. : Container()
  167. ],
  168. ),
  169. ],
  170. ),
  171. );
  172. }
  173. }