Flutter / Android - moving focus from a TextField to a DropdownButton

后端 未结 3 1113
醉梦人生
醉梦人生 2021-02-14 14:35

I have a textfield and a dropdownbutton on a screen. When I move from the textfield to choose an item and then back to the textfield I find this a little bit awkward.

相关标签:
3条回答
  • 2021-02-14 14:47

    Sorry for the late Answer... I'm also finding the solution for this. now i get it.

    Only you have to add this

     FocusScope.of(context).requestFocus(new FocusNode());
    

    in your

    _dropdownChange(val) method

    void _dropdownChange(val) {
     setState(() {
       FocusScope.of(context).requestFocus(new FocusNode());///It will clear all focus of the textfield
       selectedDropdown = val;
     });
    }
    

    also refer Abdul Wahab answer

    0 讨论(0)
  • 2021-02-14 14:51

    There's currently an ongoing issue in the flutter GitHub repo about this problem. It isn't solved as of yet, but there's a convenient workaround in place this comment implies.

    You can add FocusManager.instance.primaryFocus.unfocus(); to the onTap() event of your DropdownButton that should 'steal' focuses off of the TextField that was previously focused on the dropdown tap.

    0 讨论(0)
  • 2021-02-14 14:52

    @Dithest answer is correct but one problem still in this use case is when user tap outside of drop down menu then text field refocus and keyboard open . My work around of this issue is to add following code as well in onTap Listener of DropDownButton

    FocusScope.of(context).requestFocus(new FocusNode());///It will clear all focus of the textfield

    0 讨论(0)
提交回复
热议问题