ix_text_field.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. required 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: Icon(
  165. widget.prefixIcon,
  166. color: Get.reactiveTheme.hintColor,
  167. size: 20,
  168. ),
  169. suffixIcon: _buildSuffixIcon(),
  170. border: InputBorder.none,
  171. contentPadding: const EdgeInsets.symmetric(
  172. vertical: 12,
  173. horizontal: 16,
  174. ),
  175. isDense: true,
  176. ),
  177. cursorColor: Get.reactiveTheme.shadowColor,
  178. ),
  179. ),
  180. if (!_hasError && _hasFocus && widget.tipText != null)
  181. Padding(
  182. padding: EdgeInsets.only(top: 10.w),
  183. child: Text(
  184. widget.tipText!,
  185. style: TextStyle(
  186. color: Get.reactiveTheme.shadowColor,
  187. fontSize: 13.sp,
  188. height: 1.4,
  189. fontWeight: FontWeight.w400,
  190. ),
  191. ),
  192. ),
  193. if (widget.errorText != null && _hasError)
  194. Padding(
  195. padding: EdgeInsets.only(top: 10.w),
  196. child: Text(
  197. widget.errorText!,
  198. style: TextStyle(
  199. color: DarkThemeColors.errorColor,
  200. fontSize: 13.sp,
  201. height: 1.4,
  202. fontWeight: FontWeight.w400,
  203. ),
  204. ),
  205. ),
  206. ],
  207. );
  208. }
  209. Widget? _buildSuffixIcon() {
  210. if (widget.isPassword) {
  211. return Row(
  212. mainAxisSize: MainAxisSize.min,
  213. mainAxisAlignment: MainAxisAlignment.end,
  214. children: [
  215. if (widget.showClearButton && widget.controller.text.isNotEmpty)
  216. ClickOpacity(
  217. child: Icon(
  218. Icons.clear,
  219. color: Get.reactiveTheme.hintColor,
  220. size: 20,
  221. ),
  222. onTap: () {
  223. widget.controller.clear();
  224. if (widget.onChanged != null) {
  225. widget.onChanged!('');
  226. }
  227. setState(() {
  228. _hasError = false;
  229. });
  230. },
  231. ),
  232. 8.horizontalSpace,
  233. ClickOpacity(
  234. child: Icon(
  235. _obscureText ? Icons.visibility_off : Icons.visibility,
  236. color: Get.reactiveTheme.hintColor,
  237. size: 20,
  238. ),
  239. onTap: () {
  240. setState(() {
  241. _obscureText = !_obscureText;
  242. });
  243. },
  244. ),
  245. 16.horizontalSpace,
  246. ],
  247. );
  248. } else if (widget.showClearButton && widget.controller.text.isNotEmpty) {
  249. return Row(
  250. mainAxisSize: MainAxisSize.min,
  251. mainAxisAlignment: MainAxisAlignment.end,
  252. children: [
  253. ClickOpacity(
  254. child: Icon(
  255. Icons.clear,
  256. color: Get.reactiveTheme.hintColor,
  257. size: 20,
  258. ),
  259. onTap: () {
  260. widget.controller.clear();
  261. if (widget.onChanged != null) {
  262. widget.onChanged!('');
  263. }
  264. setState(() {
  265. _hasError = false;
  266. });
  267. },
  268. ),
  269. 16.horizontalSpace,
  270. ],
  271. );
  272. }
  273. return null;
  274. }
  275. }