美文网首页
flutter TextField自带删除按钮添加更多按钮

flutter TextField自带删除按钮添加更多按钮

作者: 9九9 | 来源:发表于2019-12-25 17:20 被阅读0次

    要求使用flutter实现如图textfiled功能
    初识flutter欢迎交流。

    image.png image.png

    使用方法:

    JJText(
                fieldCallBack: (v) {
                  print("Callback ${v}");
                },
                isRightBtn: true,
                rightIcon: Image.asset('images/login/密码可见.png'),
                icon: Image.asset(
                  'images/login/手机.png',
                  fit: BoxFit.cover,
                ),
                
                height: 100,
                text: "用户名",
                onRightBtnClick: () {
                  print("点我了");
                },
              ),
    

    以下代码为组建:

    import 'package:flutter/material.dart';
    
    typedef void JJTextFieldCallBack(String content);
    
    class JJText extends StatefulWidget {
      final String text;
      final bool password;
      // 是否有更多按钮
      final bool isRightBtn;
      // 是否显示取消按钮
      bool isShowClean;
      final Object onChanged;
      // 更多按钮点击
      final Object onRightBtnClick;
      final int maxLines;
      final double height;
      // 左侧按钮图标
      final Widget icon;
      // 右侧更多按钮图标
      final Widget rightIcon;
    
      final JJTextFieldCallBack fieldCallBack;
    
      JJText({
        Key key,
        this.text = "输入内容",
        this.password = false,
        this.isShowClean = false,
        this.onChanged,
        this.onRightBtnClick,
        this.maxLines = 1,
        this.height = 68,
        this.icon,
        this.rightIcon,
        this.isRightBtn = false,
        this.fieldCallBack,
      }) : super(key: key);
    
      @override
      _JJTextaState createState() => _JJTextaState();
    }
    
    class _JJTextaState extends State<JJText> {
      FocusNode _focusNode = new FocusNode();
      TextEditingController controller = TextEditingController();
      @override
      void initState() {
        super.initState();
        _focusNode.addListener(_focusNodeListener);
      }
    
      Future<Null> _focusNodeListener() async {
        if (_focusNode.hasFocus) {
          setState(() {
            widget.isShowClean = true;
          });
        } else {
          setState(() {
            widget.isShowClean = false;
          });
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Container(
            child: TextField(
              focusNode: _focusNode,
              controller: controller,
              maxLines: widget.maxLines,
              obscureText: widget.password,
              decoration: InputDecoration(
                  icon: widget.icon,
                  fillColor: Colors.green,
                  suffixIcon: Container(
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        widget.isShowClean
                            ? IconButton(
                                icon: Image.asset('images/login/清除.png'),
                                onPressed: onCancel,
                              )
                            : Text(""),
                        widget.isRightBtn
                            ? IconButton(
                                icon: widget.rightIcon,
                                onPressed: widget.onRightBtnClick,
                              )
                            : Text(""),
                      ],
                    ),
                  ),
                  hintText: widget.text,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: BorderSide.none)),
              onChanged: (v) {
                widget.fieldCallBack(v);
                // if(v.length == 0){
                //   print("00000000");
                // }
                setState(() {
                  widget.isShowClean = v.isNotEmpty;
                });
              },
              onSubmitted: (v) {
                widget.fieldCallBack(v);
                setState(() {
                  widget.isShowClean = false;
                });
              },
            ),
            decoration: BoxDecoration(
                border:
                    Border(bottom: BorderSide(width: 1, color: Colors.black12))),
          ),
        );
      }
    
      onCancel() {
        // 保证在组件build的第一帧时才去触发取消清空内
        WidgetsBinding.instance.addPostFrameCallback((_) => controller.clear());
        setState(() {
          widget.isShowClean = false;
        });
      }
    }
    
    
    

    相关文章

      网友评论

          本文标题:flutter TextField自带删除按钮添加更多按钮

          本文链接:https://www.haomeiwen.com/subject/fgicoctx.html