The Widget Tree


I’ve learned some cool new things about how Flutter works behind the scenes and it has been exciting putting this knowledge into practice. Flutter builds all of an app’s widget into a tree structure, called the Widget Tree. As a best practice not only for code readability and maintainability, but, even more importantly, for performance, it is best to keep your widget tree as shallow as possible and to not have widgets deep in the tree causing rebuilds of the entire widget tree. With this new knowledge, I learned that it was best to refactor repeated widget code into widget classes rather than helper methods that return a widget. This is because a helper method causes unnecessary rebuilds of the widget tree but classes are more precise. One simple reason for this is that classes are explicitly either stateless or stateful, but there are more complex reasons which I am still trying to understand well enough to repeat to others!

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *