The following is the relevant code. The debugPrint() in the onFieldSubmitted: is running, so the code is getting here, but the focus is not changing. Focus does not change.
_createItemHeadingWidget() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _controllerItemHed,
autofocus: true,
focusNode: _focusNodeItemHed,
textInputAction: TextInputAction.next,
onFieldSubmitted: (v) {
debugPrint("ItemHeading submitted");
FocusScope.of(context).requestFocus(_focusNodeItemDetail);
},
decoration: InputDecoration(
labelText: "Item Heading",
hintText: "Enter item heading",
border: OutlineInputBorder(borderRadius: BorderRadius.circular(20.0)),
),
),
);
}
Widget _createItemDetailWidget() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _controllerItemDetail,
maxLength: 75,
focusNode: _focusNodeItemDetail,
textInputAction: TextInputAction.next,
onFieldSubmitted: (v) {
debugPrint("ItemDetail submitted");
FocusScope.of(context).requestFocus(_focusNodeItemPrice);
},
decoration: InputDecoration(
labelText: "Item Detail",
hintText: "Enter item detail",
border: OutlineInputBorder(borderRadius: BorderRadius.circular(20.0)),
),
),
);
}
class DataEntryState extends State<DataEntry> {
final TextEditingController _controllerItemHed = TextEditingController();
final TextEditingController _controllerItemDetail = TextEditingController();
final TextEditingController _controllerItemPrice = TextEditingController();
final TextEditingController _controllerSeller = TextEditingController();
final TextEditingController _controllerContactNr = TextEditingController();
final TextEditingController _controllerLocation = TextEditingController();
final FocusNode _focusNodeItemHed = FocusNode();
final FocusNode _focusNodeItemDetail = FocusNode();
final FocusNode _focusNodeItemPrice = FocusNode();
final FocusNode _focusNodeSeller = FocusNode();
final FocusNode _focusNodeContactNr = FocusNode();
final FocusNode _focusNodeLocation = FocusNode();
After trying many things that did not work, the only solution that I could find is as follows:
_changeFocus(BuildContext context, FocusNode focusNodeCurrent,
FocusNode focusNodeNext) {
focusNodeCurrent.unfocus();
setState(() => _focusNodeCurrent = focusNodeNext);
}
debugPrint() showed that in _changeFocus(), unfocus() does work, and that requestFocus() did not work.
In the build :
if (_focusNodeCurrent != null)
FocusScope.of(context).requestFocus(_focusNodeCurrent);
Other relevant code (some of which is probably redundant) :
TextFormField _createItemHeadingWidget(BuildContext context) {
FocusScope.of(context).reparentIfNeeded(_focusNodeItemHed);
return TextFormField(
controller: _controllerItemHed,
maxLength: 50,
keyboardType: TextInputType.text,
autofocus: _dataRec == null,
focusNode: _focusNodeItemHed,
textInputAction: TextInputAction.next,
onFieldSubmitted: (v) {
_changeFocus(context, _focusNodeItemHed, _focusNodeItemDetail);
},
decoration: InputDecoration(
labelText: "Item Heading",
hintText: "Enter item heading",
border: OutlineInputBorder(borderRadius: BorderRadius.circular(20.0)),
),
);
}
Use
FocusScope.of(context).unfocus();
instead of
FocusScope.of(context).requestFocus(new FocusNode());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With