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

459

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.

Share:
459
JerryZhou
Author by

JerryZhou

Updated on December 11, 2022

Comments

  • JerryZhou
    JerryZhou over 1 year

    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.
  • JerryZhou
    JerryZhou almost 5 years
    What about clear text logic, which more like the sub widget’s feature self, to implement it I include TextEditingController which need StafulWidget
  • JerryZhou
    JerryZhou almost 5 years
    And animation part I currently have a global animation to control TextField, after refactor, I can use StatefulWidget’s property for all of them, but what about animation which more like TextField self feature, to include animation controller , still need use Stateful Widget.
  • Mazin Ibrahim
    Mazin Ibrahim almost 5 years
    @JerryZhou I guess you need to make to parent StatefulWidget too, since it's the only way to alter its state using clear(). And for the animation part of the TextField, again you're right. I guess my answer need a revision.
  • JerryZhou
    JerryZhou almost 5 years
    Another way is to put most of static UI in with const when possible even in StatefulWidget to improve performance
  • Mazin Ibrahim
    Mazin Ibrahim almost 5 years
    Using const may cause problems sometimes, I remember one question here where someone was complaining about a strange in its build function. Turned out to be solved by eliminating const.
  • JerryZhou
    JerryZhou almost 5 years
    for clear() function , you prefer use only one TextEdtingController in parent widget then use as many as Textfield in each to controller it self?
  • Mazin Ibrahim
    Mazin Ibrahim almost 5 years
    You say linking all of them with one TextEditingController ?
  • JerryZhou
    JerryZhou almost 5 years
    ah , no, I mean put all TextEdtingControllers together in one parent widget
  • Mazin Ibrahim
    Mazin Ibrahim almost 5 years