简体   繁体   中英

Flutter ListTile with empty title - is it possible?

I want to have ListTile widget that sometimes has title as null, eg:

ListTile(
  title: item.title.isNotEmpty
    ? Text(
        item.title,
        maxLines: 1,
      )
    : null,
subtitle: Text(
        task.text,
        maxLines: 3,
        overflow: TextOverflow.fade,
      ),
}

If the title is null, it stays there as an empty space, which causes entire widget to look ugly.

On the other hand, if the subtitle is null, it nicely collapse.

在此处输入图像描述

You can place the subtitle text on title place when title is empty.

ListTile(
  title:Text(
        item.title.isNotEmpty? item.title : task.text,
        maxLines: 1,
      ), 
  subtitle: item.title.isNotEmpty? Text(
        task.text,
        maxLines: 3,
        overflow: TextOverflow.fade,
      ): null,
}

Yes you can.

You have an error in your code, you closed your ListTitle with this '}', replace it with ')'.

Result will be:

ListTile(
  title: item.title.isNotEmpty
    ? Text(
        item.title,
        maxLines: 1,
      )
    : null,
  subtitle: Text(
    task.text,
    maxLines: 3,
    overflow: TextOverflow.fade,
  ),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM