Search code examples
flutterperformancedartoptimization

Better way to set no padding


There are 2 ways to display padding conditionally.

The first one is to pass EdgeInsets.zero to padding constructor like this:

Padding(
  padding: ... ? EdgeInsets.zero : ...,
  child: child,
)

The other way is to wrap the child conditionally like this:

if(...)
  Padding(padding: ..., child: child)
else child

Which one is better in terms of performance?


Solution

  • First one will be more efficient because you are not rebuilding entire widget, just changing its property