ITextInput.dart 5.7 KB

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