flutterflutter-widgetstatefulwidgetstatelesswidgetdynamic-values

For creating common widgets, which one is best Statless, stateFull or simple function with named params?


If a widget has the same style all over the project. We make it common so that we can easily use that with a minimal number of lines. But which one should I use to create a common widget.

Let's assume I have a text widget and text form field widget with the same style. Which one should I use?

What if the text widget has a dynamic string? and what if the text widget has a simple String?


Solution

  • According to flutter team, Widgets are better than helperMethods, refer to the video Widgets vs HelperMethods, StatelessWidgets are lighter than StatefulWidget so as long as you can use StatelessWidget over the StatefulWidget pick the StatelessWidget, but some widget might not work if it was in a StatelessWidget such as the TextField
    if you try to put a TextField inside a StatelessWidget it will clear it's value everytime you open the keyboard or a dialog pops up, so
    StatelessWidgets are the best but sometimes it wont work
    StatefulWidget are the second bes option
    helperMethods are good only if you have a widget with a shared style inside a single screen