动态更新Flutter中的keyboardType

时间:2018-11-24 14:39:56

标签: keyboard flutter

我有一个搜索TextFied和一个Tabbar,可以按数字或文本进行搜索。

当TabBar被触发/点击时,键盘类型应更新。

  • 允许正确检测类型的侦听器
  • 将触发build方法,并使用keyboardType重新构建Textfield

但是键盘类型没有更新

_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(() {
        ;
      });
    }
  }

然后使用TextField构建searchBar

_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 个答案:

答案 0 :(得分:0)

您可以使用此:

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

也许可以帮助您。