Dynamically update keyboardType in Flutter

我的梦境 提交于 2021-01-29 10:14:16

问题


I have a search TextFied and a Tabbar allowing to search by a number or a text.

When the TabBar is Fired/Tapped the keyboard type should be updated.

  • The listener allowing to detect the type is correctly fired
  • The build method is fired and the Textfield is rebuild with the keyboardType

But the keyboard type is not updated

_handleTabSelection() {
    if (_tabController.indexIsChanging) {
      switch (_tabController.index) {
        case kNameIndex:
          _searchBy = RunnersSubscriptionsSearchBy.name;
          _keyboardType = TextInputType.text;

          break;
        case kTibibIndex:
          _searchBy = RunnersSubscriptionsSearchBy.tibib;
          _keyboardType = TextInputType.number;
          //_focus.unfocus();
          break;
      }
      setState(() {
        ;
      });
    }
  }

And the Build searchBar with TextField

_buildSearchBar() {
    return Container(
      color: Theme.of(context).secondaryHeaderColor,
      child: new Padding(
        padding: const EdgeInsets.all(5),
        child: new Card(
          child: new ListTile(
            contentPadding: EdgeInsets.only(left: 8, right: 0),
            leading: new Icon(Icons.search),
            title: new TextField(
              keyboardType: _keyboardType,
              focusNode: _focus,
              controller: _searchTextFieldEditingController,
              decoration: new InputDecoration(
                  hintText: _searchHintTextFromSearchType(_searchBy),
                  border: InputBorder.none),
            ),
            trailing: new IconButton(
              icon: new Icon(Icons.cancel),
              onPressed: () {
                _searchTextFieldEditingController.clear();
                //onSearchTextChanged('');
              },
            ),
          ),
        ),
      ),
    );
  }

回答1:


You can use this:

FocusScope.of(context).requestFocus(_blankNode);
Future.delayed(Duration(seconds: 0)).then((v){
  FocusScope.of(context).requestFocus(_nameNode);
});

Maybe help you.



来源:https://stackoverflow.com/questions/53459261/dynamically-update-keyboardtype-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!