ix_text_field.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get.dart';
  5. import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';
  6. import '../../config/theme/dark_theme_colors.dart';
  7. import 'click_opacity.dart';
  8. class IXTextField extends StatefulWidget {
  9. final String hintText;
  10. final IconData? prefixIcon;
  11. final bool isPassword;
  12. final TextEditingController controller;
  13. final FocusNode? focusNode;
  14. final TextInputType keyboardType;
  15. final TextInputAction textInputAction;
  16. final Function(String)? onChanged;
  17. final bool showClearButton;
  18. final String? errorText;
  19. final bool enabled;
  20. final bool isEmail;
  21. final String? tipText;
  22. final bool Function(String)? validator;
  23. const IXTextField({
  24. super.key,
  25. required this.hintText,
  26. this.prefixIcon,
  27. this.isPassword = false,
  28. required this.controller,
  29. this.focusNode,
  30. this.keyboardType = TextInputType.text,
  31. this.textInputAction = TextInputAction.done,
  32. this.onChanged,
  33. this.showClearButton = true,
  34. this.errorText,
  35. this.enabled = true,
  36. this.isEmail = false,
  37. this.tipText,
  38. this.validator,
  39. });
  40. @override
  41. State<IXTextField> createState() => _IXTextFieldState();
  42. }
  43. class _IXTextFieldState extends State<IXTextField> {
  44. bool _obscureText = true;
  45. bool _hasFocus = false;
  46. bool _hasError = false;
  47. @override
  48. void initState() {
  49. super.initState();
  50. widget.focusNode?.addListener(_onFocusChange);
  51. // 初始验证
  52. if (widget.controller.text.isNotEmpty) {
  53. _validateInput(widget.controller.text);
  54. }
  55. }
  56. // 添加 didUpdateWidget 监听属性变化
  57. @override
  58. void didUpdateWidget(IXTextField oldWidget) {
  59. super.didUpdateWidget(oldWidget);
  60. if (widget.controller.text.isNotEmpty) {
  61. _validateInput(widget.controller.text);
  62. }
  63. }
  64. @override
  65. void dispose() {
  66. widget.focusNode?.removeListener(_onFocusChange);
  67. super.dispose();
  68. }
  69. void _onFocusChange() {
  70. setState(() {
  71. _hasFocus = widget.focusNode?.hasFocus ?? false;
  72. // 当失去焦点时进行验证
  73. if (!_hasFocus && widget.controller.text.isNotEmpty) {
  74. _validateInput(widget.controller.text);
  75. }
  76. });
  77. }
  78. void _validateInput(String value) {
  79. if (widget.validator != null) {
  80. // 使用自定义验证器
  81. setState(() {
  82. _hasError = !widget.validator!(value);
  83. });
  84. } else if (widget.isEmail) {
  85. // 默认电子邮件验证
  86. final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
  87. setState(() {
  88. _hasError = !emailRegex.hasMatch(value);
  89. });
  90. } else {
  91. setState(() {});
  92. }
  93. }
  94. @override
  95. Widget build(BuildContext context) {
  96. // 确定边框颜色
  97. Color borderColor = Get.reactiveTheme.dividerColor;
  98. Color bgColor = Get.reactiveTheme.highlightColor;
  99. if (_hasFocus) {
  100. borderColor = Get.reactiveTheme.shadowColor;
  101. bgColor = Get.reactiveTheme.highlightColor;
  102. }
  103. return Column(
  104. crossAxisAlignment: CrossAxisAlignment.start,
  105. children: [
  106. Container(
  107. decoration: BoxDecoration(
  108. color: bgColor,
  109. borderRadius: BorderRadius.circular(12.r),
  110. border: Border.all(color: borderColor, width: 1.0),
  111. ),
  112. alignment: Alignment.center,
  113. height: 50.w,
  114. child: TextField(
  115. controller: widget.controller,
  116. focusNode: widget.focusNode,
  117. obscureText: widget.isPassword && _obscureText,
  118. keyboardType: widget.keyboardType,
  119. cursorHeight: 14,
  120. enableSuggestions: false,
  121. autocorrect: false,
  122. textInputAction: widget.textInputAction,
  123. inputFormatters: widget.isPassword
  124. ? [LengthLimitingTextInputFormatter(20)]
  125. : [],
  126. onTap: () {
  127. // 不要先unfocus
  128. if (widget.focusNode != null) {
  129. widget.focusNode!.requestFocus();
  130. Future.delayed(const Duration(milliseconds: 150), () {
  131. // 每次需要点击两次才能显示键盘
  132. SystemChannels.textInput.invokeMethod('TextInput.show');
  133. });
  134. }
  135. },
  136. onChanged: (value) {
  137. if (widget.onChanged != null) {
  138. widget.onChanged!(value);
  139. }
  140. // 实时验证
  141. if (value.isNotEmpty) {
  142. _validateInput(value);
  143. } else {
  144. setState(() {
  145. _hasError = false;
  146. });
  147. }
  148. },
  149. enabled: widget.enabled,
  150. style: TextStyle(
  151. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  152. fontSize: 14,
  153. height: 1.6,
  154. fontWeight: FontWeight.w400,
  155. ),
  156. decoration: InputDecoration(
  157. hintText: widget.hintText,
  158. hintStyle: TextStyle(
  159. color: Get.reactiveTheme.hintColor,
  160. fontSize: 14,
  161. height: 1.6,
  162. fontWeight: FontWeight.w400,
  163. ),
  164. prefixIcon: widget.prefixIcon != null
  165. ? Icon(
  166. widget.prefixIcon,
  167. color: Get.reactiveTheme.hintColor,
  168. size: 20,
  169. )
  170. : null,
  171. suffixIcon: _buildSuffixIcon(),
  172. border: InputBorder.none,
  173. contentPadding: const EdgeInsets.symmetric(
  174. vertical: 12,
  175. horizontal: 16,
  176. ),
  177. isDense: true,
  178. ),
  179. cursorColor: Get.reactiveTheme.shadowColor,
  180. ),
  181. ),
  182. if (!_hasError && _hasFocus && widget.tipText != null)
  183. Padding(
  184. padding: EdgeInsets.only(top: 10.w),
  185. child: Text(
  186. widget.tipText!,
  187. style: TextStyle(
  188. color: Get.reactiveTheme.shadowColor,
  189. fontSize: 13.sp,
  190. height: 1.4,
  191. fontWeight: FontWeight.w400,
  192. ),
  193. ),
  194. ),
  195. if (widget.errorText != null && _hasError)
  196. Padding(
  197. padding: EdgeInsets.only(top: 10.w),
  198. child: Text(
  199. widget.errorText!,
  200. style: TextStyle(
  201. color: DarkThemeColors.errorColor,
  202. fontSize: 13.sp,
  203. height: 1.4,
  204. fontWeight: FontWeight.w400,
  205. ),
  206. ),
  207. ),
  208. ],
  209. );
  210. }
  211. Widget? _buildSuffixIcon() {
  212. if (widget.isPassword) {
  213. return Row(
  214. mainAxisSize: MainAxisSize.min,
  215. mainAxisAlignment: MainAxisAlignment.end,
  216. children: [
  217. if (widget.showClearButton && widget.controller.text.isNotEmpty)
  218. ClickOpacity(
  219. child: Icon(
  220. Icons.clear,
  221. color: Get.reactiveTheme.hintColor,
  222. size: 20,
  223. ),
  224. onTap: () {
  225. widget.controller.clear();
  226. if (widget.onChanged != null) {
  227. widget.onChanged!('');
  228. }
  229. setState(() {
  230. _hasError = false;
  231. });
  232. },
  233. ),
  234. 8.horizontalSpace,
  235. ClickOpacity(
  236. child: Icon(
  237. _obscureText ? Icons.visibility_off : Icons.visibility,
  238. color: Get.reactiveTheme.hintColor,
  239. size: 20,
  240. ),
  241. onTap: () {
  242. setState(() {
  243. _obscureText = !_obscureText;
  244. });
  245. },
  246. ),
  247. 16.horizontalSpace,
  248. ],
  249. );
  250. } else if (widget.showClearButton && widget.controller.text.isNotEmpty) {
  251. return Row(
  252. mainAxisSize: MainAxisSize.min,
  253. mainAxisAlignment: MainAxisAlignment.end,
  254. children: [
  255. ClickOpacity(
  256. child: Icon(
  257. Icons.clear,
  258. color: Get.reactiveTheme.hintColor,
  259. size: 20,
  260. ),
  261. onTap: () {
  262. widget.controller.clear();
  263. if (widget.onChanged != null) {
  264. widget.onChanged!('');
  265. }
  266. setState(() {
  267. _hasError = false;
  268. });
  269. },
  270. ),
  271. 16.horizontalSpace,
  272. ],
  273. );
  274. }
  275. return null;
  276. }
  277. }