如何更改列表中每个元素的背景颜色取决于每行内的值

How can I change background color of each element in list depend of value inside each row

我创建了简单的待办事项应用程序。用户可以选择任务的颜色,该颜色在任务数组中保存为字符串。我想用选定的颜色更新每个任务背景。我试过像下面这样的东西,但它不起作用。我也尝试放置 Color(day.dailyTasks.color) 但它也不起作用。谢谢。

import SwiftUI

struct ListView: View {
    
    
    @EnvironmentObject var taskVM : TaskViewModel
    
    var body: some View {
        
        NavigationView{
            
            List{
                ForEach(taskVM.sortedDays) {day in
                    Section(header:
                                Text(day.day)
                                .font(.title2)
                                .fontWeight(.light)
                    ){
                        ForEach(day.dailyTasks.sorted(by: {[=10=].hour < .hour}), id: \.hour) {task in
                               
                            HStack{
                    
                                Image(systemName: task.complete ? "checkmark.circle.fill" : "circle")
                                    
                                Text(task.tittle)
                                    .font(.system(size: 20))
                                    .fontWeight(.light)
                                    
                                    
                                Spacer()
                                
                                Text(task.hour)
                                    .font(.system(size: 20))
                                    .fontWeight(.light)
                                
                            }
                            .swipeActions {
                                Button(role: .destructive){
                                    taskVM.deleteTask(day: day, task: task)
                                } label:{
                                    Label("Delete", systemImage: "trash.circle.fill")
                                }
                                Button{
                                    taskVM.completeTask(day: day, task: task)
                                } label:{
                                    Label("Done", systemImage: "checkmark.circle.fill")
                                }
                                .tint(.green)
                            }
                        }
                        // : Foreach (Tasks)
                        .listRowBackground(Color(task.color))  // <- Doesn't work
                        
                    
                } // ForEach (Days)
                
                
            }.navigationTitle("Your tasks")
            
        }
        
      }
    }// : ZStack
    
}

如果要更改个别行,必须将 .listRowBackground() 放在实际行上。

        List{
            ForEach(taskVM.sortedDays) {day in
                Section( ... ){
                    ForEach(day.dailyTasks.sorted(by: {[=10=].hour < .hour}), id: \.hour) {task in
                           
                        HStack{
                            ...
                        }
                        // Place it on the actual row that you want changed, otherwise
                        // you apply it to all rows.
                        // Also, this code assumes that task.color is of type Color
                        .listRowBackground(task.color)
                        .swipeActions {
                            ...
                        }
                    }
                    // : Foreach (Tasks)
            } // ForEach (Days)