i have a Widget build(BuildContext context)
which returns some Container
with elements inside, but when 'else' comes in the end in some cases i dont want to return an empty Container because it still takes some space. how can i remove these spaces?
Container
imposes zero constraints on its children and tells no information about its size to its parent.
If you're confused by this statement, before continuing reading, read this Flutter's documentation page about layouting.
This being said, I would do the following:
SizedBox.shrink()
instead of Container()
inside your else
clause. This widget tells its parent that he wants to be of size "zero" (as you've requested). NOTE. This is just for readability purposes. A parent can still force a minimum height or width onto its children;Column
, then I'd set its mainAxisAlignment
to MainAxisAlignment.start
so that now no space is supposed to be inserted between its children. There are way more possible cases though (probably infinite), so further investigation should be done.Hope this helps.