0

How to retain the focus in the TextField after clicking submit/next/done in keyboard?

This is my TextField:

TextField(
  autofocus: true,
  readOnly: !isReady,
  focusNode: qtyNode,
  controller: qtyController,
  textAlign: TextAlign.right,
  textInputAction: TextInputAction.next,
  keyboardType: TextInputType.number,
  maxLength: 4,
  onEditingComplete: () {},
  onChanged: (s) {
    try {
      ref.read(qtyProvider.state).state = int.parse(s);
    } catch (e) {
      ScaffoldMessenger.of(context)
        ..hideCurrentSnackBar()
        ..showSnackBar(
          SnackBar(
            content: Text(e.toString()),
            backgroundColor: Colors.red,
          ),
        );
    }
  },
  onSubmitted: (x) async {
    ref
        .read(formDataProvider.notifier)
        .updateSelected(context, qtyController.text);
    qtyNode.requestFocus();
  },
  maxLengthEnforcement: MaxLengthEnforcement.enforced,
  inputFormatters: [
    FilteringTextInputFormatter.digitsOnly
  ],
);

When the onSubmitted callback is called, I update my provider(riverpod) wit the latest value of the QTY entered by user. Then I go to the next data in the ListView and mark the next data as the new selected item.

But when I go to the next record, I still want to remain focus on my TextField.

I tried adding this solution but the behavior is different. The keyboard is still shown but when I click the numbers, it wont reflect to my TextField.

Update

It seems like a flutter issue, I made an issue in github and got redirected in this thread. You can follow up there if you also experience the same issue.

1 Answers1

0

You need to use FocusNode for this. It's documented here with examples.

  • it seems like a flutter issue, I made an issue in github and redirected in this [thread](https://github.com/flutter/flutter/issues/98352). you can follow up here if you also experience the same issue. – Ryan baltazar Feb 21 '22 at 05:11