ITextInput.dart 4.8 KB

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