1

I am developing a flutter app, and I need to create text input just like that

enter image description here

the idea is there is an endpoint I should call to check if the username is available or note, I need to request the endpoint once the user ends typing the username.

so, the question is how to request the endpoint once, when the user done typing the username. any idea??

Hazar Belge
  • 1,009
  • 4
  • 20
Mohamad Jabaly
  • 119
  • 2
  • 10
  • Try using a FocusNode. See: https://stackoverflow.com/questions/47965141/how-to-listen-focus-change-in-flutter – Tom Truyen Aug 29 '21 at 15:41

1 Answers1

1

You can use onEditingComplete property of TextField. You can check the document here

TextField(
   onEditingComplete: (String keyword) {
      //do your logic
   },
)

Edit 1: My mistake. I think you want something like focus listener.

1- Add Listener to FocusNode:

@override
  void initState() {
    super.initState();
    _focusNode.addListener(_onFocusChange);
  }

  @override
  void dispose() {
    super.dispose();
    _focusNode.removeListener(_onFocusChange);
  }

2- Do your logic in _onFocusChange method.

  void _onFocusChange() {
    if (_focusNode.hasFocus) {
      //If focusNode hasFocus.
    } else {
      //If focusNode doesn't have focus.
    }
  }

That's why, if user exits the keyboard without pressing done. The Focus Node handles this situation.

Hazar Belge
  • 1,009
  • 4
  • 20
  • that property only works when the user presses done in the keyboard, what if he exits the keyboard without pressing done. – Mohamad Jabaly Aug 29 '21 at 15:30
  • Sorry, my mistake. I edited my answer, you may want to check out. Best Luck! – Hazar Belge Aug 29 '21 at 16:02
  • the _onFocusChange works fine when hasFocus workes(when I open the keyboard), but on the opposite side it did not work when I exit the keyboard, it only works when I exit the screen. any idea???? – Mohamad Jabaly Aug 30 '21 at 15:47