ITextInput.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. ///自带删除的ITextField
  4. typedef void ITextFieldCallBack(String content);
  5. enum ITextInputType {
  6. text,
  7. multiline,
  8. number,
  9. phone,
  10. datetime,
  11. emailAddress,
  12. url,
  13. password
  14. }
  15. class ITextField extends StatefulWidget {
  16. final ITextInputType keyboardType;
  17. final int maxLines;
  18. final int maxLength;
  19. final String hintText;
  20. final TextStyle hintStyle;
  21. final ITextFieldCallBack fieldCallBack;
  22. final ITextFieldCallBack fieldSubmit;
  23. final Icon deleteIcon;
  24. final InputBorder inputBorder;
  25. final Widget prefixIcon;
  26. final TextStyle textStyle;
  27. final FormFieldValidator<String> validator;
  28. final TextStyle counterStyle;
  29. final String inputText;
  30. final bool autofocus;
  31. final TextInputAction textInputAction;
  32. final EdgeInsetsGeometry contentPadding;
  33. final FocusNode focusNode;
  34. ITextField(
  35. {Key key,
  36. ITextInputType keyboardType = ITextInputType.text,
  37. this.maxLines = 1,
  38. this.maxLength,
  39. this.hintText,
  40. this.hintStyle,
  41. this.fieldCallBack,
  42. this.fieldSubmit,
  43. this.deleteIcon,
  44. this.inputBorder,
  45. this.textStyle,
  46. this.prefixIcon,
  47. this.validator,
  48. this.counterStyle,
  49. this.inputText,
  50. this.autofocus = false,
  51. this.textInputAction,
  52. this.contentPadding,
  53. this.focusNode})
  54. : assert(maxLines == null || maxLines > 0),
  55. assert(maxLength == null || maxLength > 0),
  56. keyboardType = maxLines == 1 ? keyboardType : ITextInputType.multiline,
  57. super(key: key);
  58. @override
  59. State<StatefulWidget> createState() => _ITextFieldState();
  60. }
  61. class _ITextFieldState extends State<ITextField> {
  62. String _inputText = '';
  63. bool _hasdeleteIcon = false;
  64. bool _isNumber = false;
  65. bool _isPassword = false;
  66. FocusNode _focusNode = FocusNode();
  67. ///输入类型
  68. TextInputType _getTextInputType() {
  69. switch (widget.keyboardType) {
  70. case ITextInputType.text:
  71. return TextInputType.text;
  72. case ITextInputType.multiline:
  73. return TextInputType.multiline;
  74. case ITextInputType.number:
  75. _isNumber = true;
  76. return TextInputType.number;
  77. case ITextInputType.phone:
  78. _isNumber = true;
  79. return TextInputType.phone;
  80. case ITextInputType.datetime:
  81. return TextInputType.datetime;
  82. case ITextInputType.emailAddress:
  83. return TextInputType.emailAddress;
  84. case ITextInputType.url:
  85. return TextInputType.url;
  86. case ITextInputType.password:
  87. _isPassword = true;
  88. return TextInputType.text;
  89. default:
  90. return TextInputType.text;
  91. }
  92. }
  93. ///输入范围
  94. List<TextInputFormatter> _getTextInputFormatter() {
  95. return _isNumber
  96. ? <TextInputFormatter>[
  97. WhitelistingTextInputFormatter.digitsOnly,
  98. ]
  99. : null;
  100. }
  101. @override
  102. void initState() {
  103. super.initState();
  104. _inputText = widget.inputText != null ? widget.inputText : '';
  105. if (widget.focusNode != null) {
  106. _focusNode = widget.focusNode;
  107. }
  108. _focusNode.addListener(() {
  109. if (!_focusNode.hasFocus) {
  110. setState(() {
  111. _hasdeleteIcon = false;
  112. });
  113. } else {
  114. setState(() {
  115. _hasdeleteIcon = (_inputText.isNotEmpty);
  116. });
  117. }
  118. });
  119. }
  120. @override
  121. Widget build(BuildContext context) {
  122. TextEditingController _controller = TextEditingController.fromValue(
  123. TextEditingValue(
  124. text: _inputText,
  125. selection: TextSelection.fromPosition(TextPosition(
  126. affinity: TextAffinity.downstream,
  127. offset: _inputText.length))));
  128. TextField textField = TextField(
  129. autofocus: widget.autofocus,
  130. focusNode: _focusNode,
  131. controller: _controller,
  132. textInputAction: widget.textInputAction,
  133. cursorColor: Theme.of(context).primaryColor,
  134. decoration: InputDecoration(
  135. counterStyle: widget.counterStyle,
  136. hintStyle: widget.hintStyle,
  137. hintText: widget.hintText,
  138. border: widget.inputBorder != null
  139. ? widget.inputBorder
  140. : UnderlineInputBorder(),
  141. fillColor: Colors.transparent,
  142. filled: true,
  143. prefixIcon: widget.prefixIcon,
  144. contentPadding: widget.contentPadding,
  145. suffixIcon: _hasdeleteIcon
  146. ? Container(
  147. width: 20.0,
  148. height: 20.0,
  149. child: IconButton(
  150. alignment: Alignment.center,
  151. padding: const EdgeInsets.all(0.0),
  152. iconSize: 18.0,
  153. icon: widget.deleteIcon != null
  154. ? widget.deleteIcon
  155. : Icon(Icons.cancel),
  156. onPressed: () {
  157. setState(() {
  158. _inputText = '';
  159. _hasdeleteIcon = (_inputText.isNotEmpty);
  160. widget.fieldCallBack(_inputText);
  161. });
  162. },
  163. ),
  164. )
  165. : Text(''),
  166. ),
  167. onChanged: (str) {
  168. setState(() {
  169. _inputText = str;
  170. _hasdeleteIcon = (_inputText.isNotEmpty);
  171. widget.fieldCallBack(_inputText);
  172. });
  173. },
  174. onSubmitted: (str) {
  175. setState(() {
  176. widget.fieldSubmit(str);
  177. });
  178. },
  179. keyboardType: _getTextInputType(),
  180. maxLength: widget.maxLength,
  181. maxLines: widget.maxLines,
  182. inputFormatters: _getTextInputFormatter(),
  183. style: widget.textStyle,
  184. obscureText: _isPassword,
  185. );
  186. return textField;
  187. }
  188. }