Widgets vs helper methods | Decoding Flutter

Flutter · 192K views · 2021 · 6:33

Open on YouTube

Web developmentSoftware architecture & designSoftware performance & optimizationexplainer

Why Stack Overflow recommends it

Stack Overflow users link it as the Flutter team's official answer when someone asks whether to extract UI into helper methods or into separate widget classes.

Recommended by 21 people across 16 different months, 2018 to 2025.

What it is

A short Decoding Flutter episode where Craig Labenz compares splitting a large build method into helper methods versus separate widget classes. He uses DartPad examples to show how rebuilds, const constructors, errors and keys behave differently in each approach.

What people said

Recommends Rémi Rousselet · 2018-11 · Stack Overflow · on “What is the difference between functions and classes to create reusable widgets?”

Edit : The Flutter team has now taken an official stance on the matter and stated that classes are preferable. See [this video] TL;DR: Prefer using classes over functions to make reusable widget-tree. EDIT : To make up for some misunderstanding: This is not about functions causing problems, but classes solving some. Flutter wouldn't have StatelessWidget [LINK] if a function could do the same thing. Similarly, it is mainly directed at public widgets, made to be reused. It doesn't matter as much for private functions made to be used only once – although being aware of this behavior is still good. There is an important difference between using functions instead of classes, that is: The framework is

Recommends dilshan · 2021-12 · Stack Overflow · on “How to reuse build method code in Flutter?”

Option (2) is the best solution. Using the below Page class you can create any number of instances(Pages) from it. class Page extends StatelessWidget { const Page({Key? key, required this.tabs, required this.tabBarViewChildren}) : super(key: key); final List<Tab> tabs; final List<Widget> tabBarViewChildren; @override build(BuildContext context) { return DefaultTabController( length: tabs.length, child: SafeArea( child: Column( children: [ SizedBox( child: TabBar( tabs: tabs, ), ), Expanded( child: TabBarView( children: tabBarViewChildren, ), ), ], ), ), ); } } In Option(1) using functions can do the same thing, but it has a number of disadvantages over classes. Read this StackOverflow to kno

Recommends Peter Koltai · 2022-02 · Stack Overflow · on “How to use PageView builder to view items based on my List?”

For the last comment see this answer [LINK] and also this video from Flutter team [this video] which is also linked to the mentioned answer.

Recommends zafercaliskan · 2022-02 · Stack Overflow · on “Flutter - How to reuse component: make a stateless or just a widget?”

There is a very good resource on this question on Flutter 's youtube channel. Widgets vs helper methods | Decoding Flutter [this video] You can also check out other videos in the playlist from here [LINK]

Recommends KuKu · 2022-04 · Stack Overflow · on “what is the difference between creating widget using widget class, and creating class widget that extend statelessWidget?”

Usually 'Home' function is called 'Helper method'. Here is a official flutter video that exlain difference between helper method and widget. [this video] The widgets in helper method is not detected in widget tree structure performance: Helper method is all rebuilded when it needs refresh. When the code is changed in Helper method, hot reload is not worked ...and so on..

Recommends Robert Sandberg · 2022-05 · Stack Overflow · on “Flutter - SetState not working on extracted widgets”

setState() actually doesn't care if it is extracted widget och method. It has no "check" for that. It tells the UI that the state of the widget has changed, and that it might be needed to redraw. It schedules a new build. But it does not necessarily redraw everything if it doesn't have to. So depending on what types of widget are below the parent widget (calling setState), and what they contain, they might be redrawn. However, a method ("extracted widget by method") is always called when redrawn, making it very un-efficient. That is why you might observe a difference. Look at this video for more information about the differences: [this video] As a rule of thumb, "always" extract widgets, not crea

Recommends Amr · 2022-05 · Stack Overflow · on “Flutter - Scaffold with Appbar and listview - breaking with Expanded”

I mean by not creating widgets from methods that don't create a method that returns a widget, but create a stateless or stateful widget and use it. check this: [this video]

Recommends Abdallah A. Odeh · 2022-06 · Stack Overflow · on “For creating common widgets, which one is best Statless, stateFull or simple function with named params?”

According to flutter team, Widgets are better than helperMethods , refer to the video Widgets vs HelperMethods [this video] , 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

Recommends Md. Yeasin Sheikh · 2022-07 · Stack Overflow · on “Difference between passing: method that returns a widget, a unique widget class, or a Widget object”

Type 1: helper methods Type 2: Widgets Type 3: Variables For type 1 and 2, I will highly recommend to check Widgets vs helper methods | Decoding Flutter [this video] Helper methods will rebuild everything on every state changes which can be heavy based on scenario like using animated-widgets. Try using Widget with const constructor to get better performance, Now for the 3rd type variable. It is totally different from helper method and widgets. This variable won't change until you handle the variable state. Test this example code, and you will find the issue where data doesn't change on type3(variable) case. class Test3 extends StatefulWidget { const Test3({Key? key}) : super(key: key); @override

Recommends Emre Faruk KOLAÇ · 2022-11 · Stack Overflow · on “Class and Stateless Widget”

Difference is actually depends on your application really but using class is more preferable in most cases. They are same, for the performance part, if you will not call setState in Widget . There are other differences like testability and accuracy. I suggest you first watch official flutter tutorial before using which one to use. Official tutorial [this video]