You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's common having to separate widgets inside a build() method.
Imagine you are building a card and want to include your very own button.
In order to separate concerns, you want to write the code for this button elsewhere, so as to simplify the Card's build method.
This can be done one of two ways: creating a helper function and creating a class (better).
Helper function
Many times we take this shortcut as it is faster to implement.
This is used all over the application.
However, it brings a set of problems, namely impacting performance:
If the user triggers a re-draw of your button, this will imply re-drawing the whole widget. As you can imagine, this will become costly for larger widgets.
classMyCardextendsStatelessWidget {
constMyCard({Key? key}) :super(key: key);
@overrideWidgetbuild(BuildContext context) {
returnContainer(
child:createButton(context),
);
}
WidgetcreateButton(context) {
// Button is created here
}
Class Widgets
What we should strive to do is create sub components inside their own separate classes.
This will improve performance, sparing CPU utilization. Moreover, class widgets become easier to test and more accurate.
classMyCardextendsStatelessWidget {
constMyCard({Key? key}) :super(key: key);
@overrideWidgetbuild(BuildContext context) {
returnContainer(
child:MyButton(),
);
}
classMyButtonextendsStatelessWidget {
constMyButton({Key? key}) :super(key: key);
@overrideWidgetbuild(BuildContext context) {
// Button is created here
}
If you have any doubts refer to this video by the Flutter team.
The text was updated successfully, but these errors were encountered:
Remove helper functions as described below.
Checklist
Uni Code Guidelines
Helper functions
It's common having to separate widgets inside a
build()
method.Imagine you are building a card and want to include your very own button.
In order to separate concerns, you want to write the code for this button elsewhere, so as to simplify the Card's build method.
This can be done one of two ways: creating a helper function and creating a class (better).
Helper function
Many times we take this shortcut as it is faster to implement.
This is used all over the application.
However, it brings a set of problems, namely impacting performance:
Class Widgets
What we should strive to do is create sub components inside their own separate classes.
This will improve performance, sparing CPU utilization. Moreover, class widgets become easier to test and more accurate.
If you have any doubts refer to this video by the Flutter team.
The text was updated successfully, but these errors were encountered: