Choosing StatelessWidget or StatefulWidget for sub-widget which has TextEditingController as a child
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.
JerryZhou
Updated on December 11, 2022Comments
-
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 :Choosing
StatelessWidget
since it have more performance, may put TextEditingController related code outside of sub widgets. Or include some logic inonChanged
event. but still there's some events likeclear text
can't put inonChanged
eventChoosing
StatefulWidget
if I useTextEditingController
include inside each sub widget.
How to choose between
StatelessWidget
andStatefulWidget
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
ofTextEdit
in parent since it's related to brother focusNode & parentNode; - Prefer to put
TextEdtingController
in parent, which will be useful in cases like whenTextField
is out of screen like aListItem
scrolled out of the screen, which will make the whole widget unmounted. The state will be preserved. - Preferring to put
animation self
like shakingTextField
when inputting something wrong in self. Since it's a feature of theTextField
not related to the parent.
-
JerryZhou almost 5 yearsWhat about clear text logic, which more like the sub widget’s feature self, to implement it I include TextEditingController which need StafulWidget
-
JerryZhou almost 5 yearsAnd 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 almost 5 years@JerryZhou I guess you need to make to
parent
StatefulWidget
too, since it's the only way to alter its state usingclear()
. And for the animation part of theTextField
, again you're right. I guess my answer need a revision. -
JerryZhou almost 5 yearsAnother way is to put most of static UI in with const when possible even in StatefulWidget to improve performance
-
Mazin Ibrahim almost 5 yearsUsing
const
may cause problems sometimes, I remember one question here where someone was complaining about a strange in itsbuild
function. Turned out to be solved by eliminatingconst
. -
JerryZhou almost 5 yearsfor 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 almost 5 yearsYou say linking all of them with one
TextEditingController
? -
JerryZhou almost 5 yearsah , no, I mean put all TextEdtingControllers together in one parent widget
-
Mazin Ibrahim almost 5 yearsLet us continue this discussion in chat.