ColoredBox vs Container with color, what's the difference?

Viewed 798

Both code does the same job:

ColoredBox(
  color: Colors.blue,
  child: SizedBox.fromSize(size: Size.fromRadius(100)),
)

And

Container(
  color: Colors.blue,
  child: SizedBox.fromSize(size: Size.fromRadius(100)),
)

So, what's the benefit of using ColoredBox when it only supports just one property color which is also provided by Container?

2 Answers

Your 2nd code

Container(color: Colors.blue, child: ...)

results in a widget hierarchy that uses a BoxDecoration to paint the background color.

BoxDecoration also covers many cases other than just painting a background color and hence it isn't as efficient as ColoredBox widget, which only paints a background color.


TL;DR:

If your use case is to only provide a background color, go for ColoredBox and not a Container.

Related