ITextInput.dart 4.4 KB

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