[SOLVED] Assign widget to class variable to improve performance?


I have a custom CircularProgressIndicator widget wrapped inside the Padding widget. I want to use this custom widget say 100 times inside build method of another (main) widget.
To avoid repetition in code I have assigned it to top level class variable, and then referenced it in wherever I needed.
Apart from code readability and preventing repetition,
will this also improve overall performance of my "main" widget?


TLDR Extract your widget to a class (see here why) and try to create a const constructor for it.

Why those two advices:

  1. Widgets created from classes can be optimized by the flutter framework among other benefits.
  2. const Objects are created at compile time and for the same configuration the same object is returned.
const CustomCircularProgressIndicator()

Say you have your widget constructor like above, anywhere you use this CustomCircularProgressIndicator the same instance of the widget will be used.

Those aren’t all the optimizations that can be done but they are a good start. For more optimizations like scoping your UI rebuilds see this great answer

Answered By – croxx5f

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

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