| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- ///自带删除的ITextField
- typedef void ITextFieldCallBack(String content);
- enum ITextInputType { text, multiline, number, phone, datetime, emailAddress, url, password }
- class ITextField extends StatefulWidget {
- final ITextInputType keyboardType;
- final int maxLines;
- final int maxLength;
- final String hintText;
- final TextStyle hintStyle;
- final ITextFieldCallBack fieldCallBack;
- final ITextFieldCallBack fieldSubmit;
- final Icon deleteIcon;
- final InputBorder inputBorder;
- final Widget prefixIcon;
- final TextStyle textStyle;
- final FormFieldValidator<String> validator;
- final TextStyle counterStyle;
- final String inputText;
- final bool autofocus;
- final TextInputAction textInputAction;
- final EdgeInsetsGeometry contentPadding;
- final FocusNode focusNode;
- final GestureTapCallback fileOnTap;
- final bool hasCancel;
- ITextField(
- {Key key,
- ITextInputType keyboardType = ITextInputType.text,
- this.maxLines = 1,
- this.maxLength,
- this.hintText,
- this.hintStyle,
- this.fieldCallBack,
- this.fieldSubmit,
- this.deleteIcon,
- this.inputBorder,
- this.textStyle,
- this.prefixIcon,
- this.validator,
- this.counterStyle,
- this.inputText = '',
- this.autofocus = false,
- this.textInputAction,
- this.contentPadding,
- this.focusNode,
- this.fileOnTap,
- this.hasCancel = false})
- : assert(maxLines == null || maxLines > 0),
- assert(maxLength == null || maxLength > 0),
- keyboardType = maxLines == 1 ? keyboardType : ITextInputType.multiline,
- super(key: key);
- @override
- State<StatefulWidget> createState() => _ITextFieldState();
- }
- class _ITextFieldState extends State<ITextField> {
- String _inputText = '';
- bool _hasdeleteIcon = false;
- bool _isNumber = false;
- bool _isPassword = false;
- FocusNode _focusNode = FocusNode();
- ///输入类型
- TextInputType _getTextInputType() {
- switch (widget.keyboardType) {
- case ITextInputType.text:
- return TextInputType.text;
- case ITextInputType.multiline:
- return TextInputType.multiline;
- case ITextInputType.number:
- _isNumber = true;
- return TextInputType.number;
- case ITextInputType.phone:
- _isNumber = true;
- return TextInputType.phone;
- case ITextInputType.datetime:
- return TextInputType.datetime;
- case ITextInputType.emailAddress:
- return TextInputType.emailAddress;
- case ITextInputType.url:
- return TextInputType.url;
- case ITextInputType.password:
- _isPassword = true;
- return TextInputType.text;
- default:
- return TextInputType.text;
- }
- }
- ///输入范围
- List<TextInputFormatter> _getTextInputFormatter() {
- return _isNumber
- ? <TextInputFormatter>[
- WhitelistingTextInputFormatter.digitsOnly,
- ]
- : null;
- }
- @override
- void initState() {
- super.initState();
- _inputText = widget.inputText != null ? widget.inputText : '';
- if (widget.focusNode != null) {
- _focusNode = widget.focusNode;
- }
- _focusNode.addListener(() {
- if (!_focusNode.hasFocus) {
- setState(() {
- _hasdeleteIcon = false;
- });
- } else {
- setState(() {
- _hasdeleteIcon = (_inputText.isNotEmpty);
- });
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- TextEditingController _controller = TextEditingController.fromValue(
- TextEditingValue(text: _inputText, selection: TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: _inputText.length))));
- TextField textField = TextField(
- autofocus: widget.autofocus,
- focusNode: _focusNode,
- controller: _controller,
- textInputAction: widget.textInputAction,
- cursorColor: Theme.of(context).primaryColor,
- decoration: InputDecoration(
- counterStyle: widget.counterStyle,
- hintStyle: widget.hintStyle,
- hintText: widget.hintText,
- border: widget.inputBorder != null ? widget.inputBorder : UnderlineInputBorder(),
- fillColor: Colors.transparent,
- filled: true,
- prefixIcon: widget.prefixIcon,
- contentPadding: widget.contentPadding,
- suffixIcon: _hasdeleteIcon
- ? Container(
- width: 18.0,
- height: 18.0,
- child: IconButton(
- alignment: Alignment.center,
- padding: const EdgeInsets.all(2.0),
- iconSize: 18.0,
- icon: widget.deleteIcon != null ? widget.deleteIcon : Icon(Icons.cancel),
- color: Color(0xFf727785),
- onPressed: () {
- setState(() {
- _inputText = '';
- _hasdeleteIcon = (_inputText.isNotEmpty);
- widget.fieldCallBack(_inputText);
- });
- },
- ),
- )
- : Text(''),
- ),
- onChanged: (str) {
- setState(() {
- _inputText = str;
- _hasdeleteIcon = (_inputText.isNotEmpty);
- widget.fieldCallBack(_inputText);
- });
- },
- onSubmitted: (str) {
- setState(() {
- widget.fieldSubmit(str);
- });
- },
- keyboardType: _getTextInputType(),
- maxLength: widget.maxLength,
- maxLines: widget.maxLines,
- inputFormatters: _getTextInputFormatter(),
- style: widget.textStyle,
- obscureText: _isPassword,
- onTap: () {
- widget.fileOnTap();
- },
- );
- if (!widget.hasCancel) {
- return textField;
- } else {
- return Row(
- children: <Widget>[
- Expanded(
- flex: 1,
- child: Container(
- height: 34,
- padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
- decoration: BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.all(Radius.circular(100)),
- ),
- child: textField),
- ),
- InkWell(
- child: Container(
- padding: EdgeInsets.symmetric(horizontal: 15, vertical: 6),
- child: Text('取消', style: TextStyle(fontSize: 16, color: Colors.white)),
- ),
- onTap: () {
- Navigator.of(context).pop(true);
- // setState(() {
-
- // _inputText = '';
- // _hasdeleteIcon = (_inputText.isNotEmpty);
- // widget.fieldCallBack(_inputText);
- // _focusNode.unfocus();
-
- // });
- },
- )
- ],
- );
- }
- }
- }
|