1

I was wondering Container has it own property align but it also has unnecessary properties that might affect the speed and memory of the app. Instead of it, we could use SizedBox and Align which do not have many properties. Or doesn't it really matter?

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
ulukbek
  • 483
  • 4
  • 10
  • duplicate https://stackoverflow.com/questions/55716322/flutter-sizedbox-vs-container-why-use-one-instead-of-the-other – Zohaib Tariq Aug 02 '22 at 07:57
  • I would say performance loss because of using `Container` is negligible. But this is kind of subjective I suppose. – Ivo Aug 02 '22 at 08:10

1 Answers1

2

In terms of performance align and sized box is better than using a container

If in a container you pass both width and height it returns a constrained box which is basically a sized box. There would only be a negligible performance difference. If you use a sized box you inform the engine that its constrained and avoid condition check in the container.

Container

constraints =
        (width != null || height != null)
          ? constraints?.tighten(width: width, height: height)
            ?? BoxConstraints.tightFor(width: width, height: height)
          : constraints,

if (constraints != null)
      current = ConstrainedBox(constraints: constraints, child: current);

If you notice if size is not null it returns a constrained box which is a sized box again.

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30