gpt4 book ai didi

flutter - 如何将 List 的每一项与 ListView 构建器的每一项进行比较?

转载 作者:IT王子 更新时间:2023-10-29 07:16:38 24 4
gpt4 key购买 nike

我有两个名为 list1 和 list2 的列表。

list1 用于显示使用 ListView.builder 的学生列表

ListView.builder
(
itemCount: litems.length,
itemBuilder: (BuildContext ctxt, int index) {
return Text(list1[index]);
}
)

list1 包含所有学生的名字

现在 list2 只包含少数学生的姓名。如果 list2 中存在学生姓名,我需要更改学生姓名的颜色。如何将 list2 的每一项与 ListView.builder

的每一项进行比较

list2 与 list1 不同。 list2 只包含几个学生的名字。

最佳答案

如何执行此操作的示例:

ListView.builder(
itemCount: litems.length,
itemBuilder: (BuildContext ctxt, int index) {
var color;
if (list2.contains(list1[index])) {
//The name is present in list2
color = Colors.green;
} else {
//The name is not present in list2
color = Colors.black;
}
return Text(
list1[index],
style: TextStyle(
color: color,
),
);
}
)

您可以在 if/else 语句中执行更多操作来自定义您的小部件。

关于flutter - 如何将 List 的每一项与 ListView 构建器的每一项进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57188272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com