0

I have a list of some entries I want edit on focus out. I createe FocusNode for each entry, CupertinoTextField for each entry too.

var textField = (UserMotivator um) {
    var controller;
    var focusNode = new FocusNode();
    focusNode.addListener(() {
        if (!focusNode.hasFocus) {
            post(um);
        }
    });

    var controller = TextEditingController(text: um.text);

    return CupertinoTextField(
        focusNode: focusNode,
        controller: controller,
        onChanged: (String value) {
            um.text = value;
        }
    );
};

For some weird reason, in simulator (not tested on real device), when I click on many of these TextFields, I get this:

enter image description here

How do I bound a focus out even to a TextField without using FocusNode/ without having all of these cursors blinking?

Martin.
  • 10,494
  • 3
  • 42
  • 68
  • Oh and when I remove the focusNode, everything works okay. – Martin. May 07 '19 at 22:59
  • I'm sorry. When I tried to reproduced your code, it worked perfectly and I realized my conception about `FocusNode` was completely wrong. The problem seems to be that your `FocusNode`s are not being automatically unfocused when another `FocusNode` gets focused. Can you please provide a code that reproduces this error? – Hugo Passos May 08 '19 at 15:18
  • @HugoPassos I tried to do a minimal working example https://gist.github.com/genesiscz/cc8f2310c6cae59560913910fef5f55c – Martin. May 08 '19 at 15:42
  • For me this code works perfectly in both Android emulator and iOS simulator. – Hugo Passos May 08 '19 at 16:53
  • What's version of your flutter? I am on [✓] Flutter (Channel beta, v1.1.8, on Mac OS X 10.14.3 18D109, locale cs-CZ) – Martin. May 08 '19 at 17:00
  • v1.2.1, channel stable. I recommend updating your Flutter. Perhaps this was an issue that has been fixed in the latest versions. – Hugo Passos May 08 '19 at 17:01
  • it still happens on stable channel, v1.5.4-hotfix.2, iPhone XR, very weird. – Martin. May 08 '19 at 17:27
  • And just tried that on my physical device and works fine. The only problem is when i click on the textfield it does move the cursor at the start, not to the end. Thanks for help – Martin. May 08 '19 at 17:52
  • Glad you could make it, but this behavior is not expected anyway. Just to point you in the right direction in order to fix that, `TextSelection` inside `TextEditingController` is the one responsible for the cursor position. – Hugo Passos May 08 '19 at 18:10

1 Answers1

0

So I resolved the issue I think. The reason it was buggy was that I was on v1.1.8, after updating to v1.5.4 it somehow got fixed, was not perfect but was better. After I moved the FocusNodes creation code form build to initState method it got even better but the cursor was still blinking at the start of the TextField. This was because I had called setState in the onChange handler, which somehow caused the TextField to redraw and act so weirdly.

Martin.
  • 10,494
  • 3
  • 42
  • 68