ITextInput.dart 5.0 KB

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