Home > other >  Which is efficient among `Padding` vs `SizedBox`?
Which is efficient among `Padding` vs `SizedBox`?

Time:12-09

Background:

SizedBox class can be used to insert empty space between widgets, like SizedBox(height: 8), and also with Padding class using EdgeInsets class,

SizedBox cannot be used everywhere.

I know this, and also where ever it can be, its use is different than Padding,

But

Im interested in:

Using ListView.builder with each child wrapped in Padding(padding: EdgeInsets.symmetric(vertical: 4)),

vs

Using ListView.separated with SizedBox(height: 8) as separator,

Reason behind curiosity:

Padding has to create two(top and bottom) empty spaces, instead of one, as in the case of SizedBox,

The Real question:

If we ignore the cases where Padding can be used, but not SizedBox, then which one would be more efficient and recommended way to achieving empty space,

thanking you,

CodePudding user response:

I think it depends on your use case, but from experience. Sized Box is better and more performant than Padding for just an empty space.

Also depending on the use case, you might find it appropriate to make use of other widgets like Spacer. Spacer is best for creating an empty space in a Row. This is because you do not need to constrain its size but just optionally provide a flex value. This way, it becomes more useful than both Padding and SizedBox.

CodePudding user response:

The thing you should take in consider is SizedBox Allow you applies constraints to it's child.

About empty spaces, they do the same thing literally. At most you'll have a nanosecond worth of difference, especially if both in your code was a const,

About the ListView.builder or ListView.separated, both have a use cases depend on what you want to do, Like separated Builder allow you to customize widget for each item, etc.

in case in your question, Use whatever fits you the best. The gain is so minimal you'll never ever notice a difference. So take what is clearer.

  • Related