dart 列表项值的常量值无效,我正在尝试确定其布尔值以决定列表分块的字体粗细

m0rkklqb  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(119)

item.header的常量错误消息无效。
它是一个布尔值,表示列表块将使用粗体还是普通字体粗细。

child: ListTile(
        key: ValueKey(item.stuffId),
        title: Text(
          item.name,
          style: const TextStyle(
            color: Colors.blue,           
            fontWeight: item.header == true ? FontWeight.bold : FontWeight.normal,                   
          ),
        ),

下面是我使用的列表:

final List<Stuff> _items = [
    Stuff(stuffId: "1", name: "Toiletries", header: true),
    Stuff(stuffId: "2", name: "Toothpaste", header: false),
    Stuff(stuffId: "3", name: "Hair brush", header: false),
    Stuff(stuffId: "4", name: "Nail clippers", header: false),
  ];

enter image description here
“化妆品”是标题,因此应该是粗体。

dgsult0t

dgsult0t1#

ListTile(
        key: ValueKey(item.stuffId),
        title: Text(
          item.name,
          style: TextStyle( // 👈 remove const here 
            color: Colors.blue,           
            fontWeight: item.header == true ? FontWeight.bold : FontWeight.normal,                   
          ),
        ),
vh0rcniy

vh0rcniy2#

item.header将获取/检查运行时,它不能是常量,请删除const

style: TextStyle(

了解更多关于What is the difference between the "const" and "final" keywords in Dart?的信息

相关问题