Flutter -TextField Controller gets my cursor before Text

6,684

i have same problem, My Task i want to clear my textfield on button click

here is my code

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,
                  )
                ],
              )

i solved by USING 'searchEditor.clear();' inside Future.delayed Because when we call FocusScope.of(context).unfocus(); for closing keyboard it take some microseconds for close that's why it show this WANRING i.e. getTextBeforeCursor on inactive InputConnection to overcome i called searchEditor.clear(); method after some microseconds

it's work for me.

Share:
6,684
GoPro
Author by

GoPro

Look for Eijun - the ace!

Updated on December 06, 2022

Comments

  • GoPro
    GoPro over 1 year

    I get this warnings on my Console W/IInputConnectionWrapper(25185): getTextBeforeCursor on inactive InputConnection W/IInputConnectionWrapper(25185): getSelectedText on inactive InputConnection W/IInputConnectionWrapper(25185): getTextAfterCursor on inactive InputConnection

    I am using dateTime keyboard, when I enter text in textfield the cursor moves before the Text. This is annoying. I read somewhere that the TextEditing controller needs to be disposed/ closed. I am not sure how and what does that mean. Any one knows how to do away with these warnings and weird behavior on the TextInputController.