代码之家  ›  专栏  ›  技术社区  ›  MistyD Harish Kumar Kailas

flutter:textfield和focusnode在textfield外单击时不被调用

  •  3
  • MistyD Harish Kumar Kailas  · 技术社区  · 5 年前

    我有一个包含文本字段的Statefull小部件。我想知道用户在文本字段外单击的时间。为了做到这一点,我使用了一个focusnode。 现在的问题是,当用户单击texfield外部时,我的回调不会被调用。这是我的密码

    class TestText extends State<SingleFieldEdit> 
    {
        TextEditingController _qTextEditController = new TextEditingController();
        FocusNode _focus;
    
        TestText();
    
        @override
        void initState() {
            super.initState();
            _focus = new FocusNode();
            _focus.addListener(_onFocusChange);
        }
    
        @override
        void dispose() {
            super.dispose();
            _focus.dispose();
        }
    
        void _onFocusChange(){
            print("Focus changed - Does not get called when clicked outside of textbox");
            if(_focus.hasFocus==false){
                setState(() {
                    _enableEdit=false;
                    _qTextEditController.text = _existingText;
                });
    
            }
        }
    
    
        Widget getEditableField()
        {
            Widget containerBorder =  new Container(
                    margin: const EdgeInsets.all(0.0),
                    padding: const EdgeInsets.only(top:0.0,bottom:0.0),
                    decoration: new BoxDecoration(
                        border: new Border(top:BorderSide(width: 0.4),bottom: BorderSide(width: 0.4) ),
                        ),
                    child: new Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                     new Flexible(child:TextField(
                            focusNode: _focus,
                            controller: _qTextEditController,
                            decoration: InputDecoration.collapsed(hintText: 'Your hint goes here',enabled: _enableEdit),//InputDecoration(hintText: 'Your hint goes here', /*labelText: "Your Label goes here",*/ enabled: _enableEdit),
                            )),
                        getIcon(),
                        ],)
                    );
    
            return getEditableField
        }
    
        @override
        Widget build(BuildContext context)
        {
            return getEditableField();
        }
    
    }
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   Günter Zöchbauer    5 年前

    你需要专注于另一个 FocusNode
    或呼叫 unfocus 聚焦论 焦点节点 ( https://github.com/flutter/flutter/issues/19552#issuecomment-410909751 )

    请跟进并投票 https://github.com/flutter/flutter/issues/20227 为了更简单的解决方案(也许还有 https://github.com/flutter/flutter/issues/7247 )

        2
  •  3
  •   Fellipe Malta    5 年前

    用手势检测器包裹脚手架,并将ONTAP设置为聚焦于另一个节点。