动态更新Flutter中的keyboardType [英] Dynamically update keyboardType in Flutter

查看:73
本文介绍了动态更新Flutter中的keyboardType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

  • 可以正确触发类型的侦听器
  • 将触发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

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('');
              },
            ),
          ),
        ),
      ),
    );
  }

推荐答案

您可以使用:

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

也许可以帮助您.

这篇关于动态更新Flutter中的keyboardType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆