【问题标题】:Flutter -TextField Controller gets my cursor before TextFlutter -TextField 控制器在文本之前获取我的光标
【发布时间】:2019-01-01 12:34:44
【问题描述】:

我在控制台上收到此警告 W/IInputConnectionWrapper(25185): getTextBeforeCursor on inactive InputConnection W/IInputConnectionWrapper(25185): getSelectedText on inactive InputConnection W/IInputConnectionWrapper(25185): getTextAfterCursor on inactive InputConnection

我正在使用 dateTime 键盘,当我在文本字段中输入文本时,光标会移动到文本之前。这很烦人。我在某处读到需要处置/关闭 TextEditing 控制器。我不确定这意味着什么以及如何。任何人都知道如何消除 TextInputController 上的这些警告和怪异行为。

【问题讨论】:

  • 很抱歉重新提出这个问题,但您解决了这个问题吗?

标签: dart material-design flutter flutter-layout flutter-dependencies


【解决方案1】:

我有同样的问题, 我的任务 我想在按钮单击时清除我的文本字段

这是我的代码

String searchData = "";
 TextEditingController searchEditor = TextEditingController();

            Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(  // My textbox
                      controller: searchEditor,
                      autofocus: false,
                      onChanged: (str){
                        searchData = (str == null)? "" : str;
                        setState(() {
                        });
                      },
                      decoration: InputDecoration(
                        prefixIcon: IconTheme(
                          child: Icon(Icons.search),
                          data: IconThemeData(color: Colors.blueAccent),
                        ),
                        labelText: 'Search',
                      ),
                    ),
                    flex: 8,
                  ),
                  Expanded(
                    child: RaisedButton(
                      color: Basic.hbColor,
                      textColor: Colors.white,
                      child: Icon(Icons.clear),
                      onPressed: (){      // On button click
                        FocusScope.of(context).unfocus();   // Close keyboard
                        setState(() {
                          searchData = "";
                        });
                        Future.delayed(Duration(microseconds: 500),(){   //call back after 500  microseconds
                          searchEditor.clear();  // clear textfield 
                        });
                      },
                      elevation: 10,
                    ),
                    flex: 2,
                  )
                ],
              )

我通过 USING 'searchEditor.clear();' 解决了在 Future.delayed 内 因为当我们调用 FocusScope.of(context).unfocus(); 来关闭键盘时,关闭键盘需要几微秒,这就是它显示此 WANRING 的原因 即getTextBeforeCursor on inactive InputConnection 为了克服我叫 searchEditor.clear();几微秒后的方法

这对我有用。

【讨论】:

    【解决方案2】:

    您必须在按下按钮之前最小化/关闭键盘,否则您不会收到任何警告。

    【讨论】:

    • 您能否更具体一点,向我们展示如何在 Flutter 中做到这一点?
    【解决方案3】:

    你需要先创建变量

    <<< var _controller = TextEditingController(); >>>
    

    然后你创建文本字段的属性并传递它

    <<<controller: _controller,>>>
    

    那么当你的事情完成后就去做

    <<<_controller.clear();>>>
    

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2021-10-02
      相关资源
      最近更新 更多