skip to Main Content

Sometimes, it can be useful to show nothing where an icon would normally be, while using the same amount of space (such as in a list where only some items have icons).

Does Flutter let you do this easily?

2

Answers


  1. Chosen as BEST ANSWER

    The Icon widget allows setting the icon to null, which achieves the desired behaviour.

    From the docs:

    The icon can be null, in which case the widget will render as an empty space of the specified size.

    For example, with a nullable widget myIcon:

    myIcon ?? const Icon(null)
    

  2. Another solution is to use the Visibility widget, which allows you to hide any widget based on a straightforward condition

     Visibility(
                visible: false, // This will occupy space but not show the icon
                child: Icon(Icons.star),
              ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search