flutterandroid-layouticonbutton

Flutter: Conditional statement for my IconButton is not working


I only want my iconButton to show if there is more info on my row. What is the correct way to code this? My if has red error mark on it.

                  if(item.info != ""){
                          child: IconButton(
                          icon:
                          const Icon(Icons.info_outline_rounded),
                               onPressed: () {
                      
                                 Navigator.push(
                                   context,
                                   MaterialPageRoute(
                                   builder: (context) => const Page2()));
                               }, //onPressed
                          ), //IconButton
                 }, //end if statement
     ), //Center

Solution

  • Center(
      child: item.info != ""
          ? IconButton(
              icon: const Icon(Icons.info_outline_rounded),
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => const Page2()));
              }, //onPressed
            )
          : Container(),
    )
    

    or with Visibility widget

    Visibility(
        visible: item.info != "",
        child: IconButton(
          icon: const Icon(Icons.info_outline_rounded),
          onPressed: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => const Page2()));
          }, //onPressed
        ))