Search code examples
flutterstatefulwidgetstatelesswidget

Choosing StatelessWidget or StatefulWidget for sub-widget which has TextEditingController as a child


I'm working on a refactored code to separated small piece of widgets. There's several columns with TextFields in each one of them.I have two options to complete the task :

  1. Choosing StatelessWidget since it have more performance, may put TextEditingController related code outside of sub widgets. Or include some logic in onChanged event. but still there's some events like clear text can't put in onChanged event

  2. Choosing StatefulWidget if I use TextEditingController include inside each sub widget.

How to choose between StatelessWidget and StatefulWidget when refactoring code into small widgets like this ?


Update

Here's my current conclusion which can be updated if it's not correct.

  • Prefer to put FocusNode of TextEditin parent since it's related to brother focusNode & parentNode;
  • Prefer to put TextEdtingController in parent, which will be useful in cases like when TextField is out of screen like a ListItem scrolled out of the screen, which will make the whole widget unmounted. The state will be preserved.
  • Preferring to put animation self like shaking TextField when inputting something wrong in self. Since it's a feature of the TextField not related to the parent.

Solution

  • Edit in response to the discussion in the comments section:

    You should go with the second approach when refactoring your code into smaller StatefulWidgets. And the parent should have a function to alter the nested TextFields' content and its animation behavior by calling functions inside it, as you earlier indicated you need to manage this behavior.