问题
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