Flutter FocusNode attachment not listening on iOS physical device

Viewed 13

I have a OTP form for verifying a phone number, I set up 6 individual text form fields, each with their own focus node. Each focus node has an attachment with a onKey function.

The goal is to go to the next text field if the user enters a number into the text field, and then on the last digit/text field it runs a function to verify the OTP with firebase. This is working in the simulator, but not on my physical iPhone. I am wondering if the "event.logicalKey.debugName" is named something different on a mobile device, than it is on my computers keyboard. But for some reason I can not get this value to print to the xcode console when running the app on a physical device.

My code...

  @override
  void initState() {
    otp1 = FocusNode();
    otp1Attachment = otp1.attach(context, onKey: otp1OnKey);
    super.initState();
  }

KeyEventResult otp1OnKey(FocusNode node, RawKeyEvent event) {
    if (event.logicalKey.debugName == "Digit 1" || event.logicalKey.debugName == "Numpad 1") {
      otp1Controller.text = "1";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 2" || event.logicalKey.debugName == "Numpad 2") {
      otp1Controller.text = "2";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 3" || event.logicalKey.debugName == "Numpad 3") {
      otp1Controller.text = "3";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 4" || event.logicalKey.debugName == "Numpad 4") {
      otp1Controller.text = "4";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 5" || event.logicalKey.debugName == "Numpad 5") {
      otp1Controller.text = "5";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 6" || event.logicalKey.debugName == "Numpad 6") {
      otp1Controller.text = "6";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 7" || event.logicalKey.debugName == "Numpad 7") {
      otp1Controller.text = "7";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 8" || event.logicalKey.debugName == "Numpad 8") {
      otp1Controller.text = "8";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 9" || event.logicalKey.debugName == "Numpad 9") {
      otp1Controller.text = "9";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Digit 0" || event.logicalKey.debugName == "Numpad 0") {
      otp1Controller.text = "0";
      otp2.requestFocus();
    } else if (event.logicalKey.debugName == "Backspace" || event.logicalKey.debugName == "Delete") {
      otp1Controller.text = "";
    }
    print(event.logicalKey.debugName);
    return KeyEventResult.handled;
  }

This is a really long function, I am open to a better way to achieve my desired result if anyone has a way. Thank you!

1 Answers

So I don't know why the attachment and onKey property weren't working on physical iOS device but I did come up with a solution...

I am using the onChanged property for the TextFormField to programmatically move the cursor to the next text field. And just checking to make sure what they entered was a number, before moving to next text field.

Related